gpt4 book ai didi

c# - 如何使用触发器在 Setter 值属性上绑定(bind)另一个控件?

转载 作者:太空宇宙 更新时间:2023-11-03 23:47:14 25 4
gpt4 key购买 nike

我对 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>

关于c# - 如何使用触发器在 Setter 值属性上绑定(bind)另一个控件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27226452/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com