【问题标题】:How to Bind an another Control on the Setter Value Property by using a Trigger?如何使用触发器在 Setter Value 属性上绑定另一个控件?
【发布时间】:2014-12-01 10:33:43
【问题描述】:

我对 WPF 中的数据绑定有点问题,希望您能帮助我。

我想在 TextBlock 上绑定 DatePicker 的 SelectedDate 参数,但仅限于选中 CheckBox 时。 CheckBox 和 TextBlock 在 DataView 中,DatePicker 在外面。

此刻我尝试将它与触发器一起使用,并在 Setter 部分的值属性中设置绑定。

<TextBlock Text="">
    <TextBlock.Style>
        <Style TargetType="TextBlock">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=IsChecked}" Value="True">
                    <Setter Property="Text" Value="{Binding ElementName=StandartPitBis, Path=SelectedDate, StringFormat='dd.MM.yyyy'}" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

但这行不通。有谁知道我能做什么?

这是我正在使用的代码部分:

GridView 内部

复选框

<GridViewColumn>
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <CheckBox Content="" x:Name="check_Anlage" IsChecked="{Binding Path=IsChecked}" />
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

文本块

<TextBlock Text="">
    <TextBlock.Style>
        <Style TargetType="TextBlock">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=IsChecked}" Value="True">
                    <Setter Property="Text" Value="{Binding ElementName=StandartPitBis, Path=SelectedDate, StringFormat='dd.MM.yyyy'}" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

在 GridView 之外

日期选择器

<DatePicker Grid.Column="1" Margin="0,5,0,5" SelectedDate="{x:Static sys:DateTime.Now}" x:Name="StandartPitVon" />

我想要做的是,来自 DatePicker 的 SelectedDate 显示在 TextBlock 中,但仅在 CheckBox 被选中时显示。

非常感谢

【问题讨论】:

    标签: c# wpf triggers datepicker textblock


    【解决方案1】:

    所以。我发现了问题。问题是,当在对象自身中设置属性时,它不能被覆盖。当你需要一个 Default-Value 和一个 Trigger 时,你也必须在 Style 中定义 Default-Value。

    示例:

    <TextBlock>
        <TextBlock.Style>
            <Style TargetType="TextBlock">
                <Setter Property="Text" Value="{x:Null}" />
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=IsChecked}" Value="True">
                        <Setter Property="Text" Value="{Binding ElementName=StandartPitVon, Path=SelectedDate, StringFormat='dd.MM.yyyy'}" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </TextBlock.Style>
    </TextBlock>
    

    【讨论】:

      猜你喜欢
      • 2011-02-01
      • 2012-03-24
      • 1970-01-01
      • 1970-01-01
      • 2013-04-20
      • 1970-01-01
      • 2015-07-12
      • 2013-10-27
      • 2015-09-23
      相关资源
      最近更新 更多