我对 WPF 中的数据绑定(bind)有一些小问题,希望你能帮助我。
我想将 DatePicker 的 SelectedDate 参数绑定(bind)到 TextBlock 上,但仅当 CheckBox 被选中时。 CheckBox 和 TextBlock 在 DataView 中,DatePicker 在外面。
此刻我尝试将它与触发器一起使用,并在 Setter 部分的值属性中设置绑定(bind)。
<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>
文本 block
<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 被选中时。
非常感谢
请您参考如下方法:
所以。我发现了问题。问题是,当在对象自身中设置一个属性时,它就不能被覆盖。当您需要默认值和触发器时,您也必须在样式中定义默认值。
示例:
<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>
