wpf 文本块的数据触发器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20846966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:20:36  来源:igfitidea点击:

DataTrigger for Textblock

c#wpfxaml

提问by webdad3

I have a Textblock that I'm trying to change the value of the Text property if a property is True or False. The issue I'm having is that the flag could be changed on different events on the screen (onchange events from other combo boxes).

我有一个 Textblock,如果属性为 True 或 False,我将尝试更改 Text 属性的值。我遇到的问题是可以在屏幕上的不同事件上更改标志(来自其他组合框的 onchange 事件)。

I'm not sure how to get this datatrigger to work as I don't think it know when the value has been changed.

我不确定如何让这个数据触发器工作,因为我认为它不知道值何时发生变化。

<TextBlock Grid.Row="9" HorizontalAlignment="Right" Text="Some Old Value:" VerticalAlignment="Center">
    <TextBlock.Style>
        <Style TargetType="{x:Type TextBlock}">
             <Style.Triggers>
                 <DataTrigger Binding="{Binding IsChecked}" Value="False" >
                     <Setter Property="Text" Value="Different Text:"/>
                 </DataTrigger>
             </Style.Triggers>
         </Style>
     </TextBlock.Style>                                
</TextBlock>

I see in some comboboxes there is the UpdateSourceTrigger=PropertyChanged, but I don't see a way to implement that in the TextBlock.

我看到在一些组合框中有UpdateSourceTrigger=PropertyChanged,但我没有看到在 TextBlock 中实现它的方法。

回答by Rohit Vats

Firstof all set default Textin style setter, otherwise no matter whether your triggers fires successfully or not, Text won't take value from Style setter because of Dependency property value precedence order. Local value has higher precedence than style setter values.

首先Text在样式设置器中设置默认值,否则无论您的触发器是否成功触发,由于Dependency 属性值优先顺序,Text 都不会从样式设置器中获取值。本地值比样式设置器值具有更高的优先级

<TextBlock Grid.Row="9" HorizontalAlignment="Right" VerticalAlignment="Center">
    <TextBlock.Style>
        <Style TargetType="{x:Type TextBlock}">
             <Setter Property="Text" Value="Some Old Value:"/>
             <Style.Triggers>
                 <DataTrigger Binding="{Binding IsChecked}" Value="False" >
                     <Setter Property="Text" Value="Different Text:"/>
                 </DataTrigger>
             </Style.Triggers>
         </Style>
     </TextBlock.Style>                                
</TextBlock>

Second, if IsCheckedproperty resides in your ViewModel (implementing INotifyPropertyChanged) and TextBlock DataContextis rightly pointing to ViewModel instance, you don't have to worry about it.

其次,如果IsChecked属性驻留在您的 ViewModel 中(实现 INotifyPropertyChanged)并且 TextBlockDataContext正确地指向 ViewModel 实例,则您不必担心它。

Just make sure whenever property IsCheckedchanges in ViewModel a PropertyChangedevent gets raised so that UI can listen to that and updates itself.

只需确保每当IsCheckedViewModel 中的属性更改时PropertyChanged都会引发事件,以便 UI 可以侦听该事件并自行更新。