DataTrigger的文本块 [英] DataTrigger for Textblock

查看:182
本文介绍了DataTrigger的文本块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我试图改变Text属性的值,如果属性是真还是假一个TextBlock。我遇到的问题是,该标志可以在不同的事件来改变屏幕上(的onchange从其他组合框事件)。

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).

我不知道如何。得到这个datatrigger工作,我不认为它知道什么时候该值已更改

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>



我在一些组合框看到有是 UpdateSourceTrigger = PropertyChanged的,但我不明白的方式来实现,在文本块。

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

推荐答案

首先设置默认文字在风格二传手,否则无论你的触发器触发成功与否,文本将不会从风格二传手,因为的依赖项属性值优先顺序本地值的优先级高于样式的setter值

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



,如果器isChecked 属性驻留在您的视图模型(实施INotifyPropertyChanged的的)和TextBlock的的DataContext 是正确指向视图模型的实例,你不必担心吧。

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

只要确保每当视图模型属性器isChecked 更改的PropertyChanged 事件得到提高,使UI可以收听和自我更新。

Just make sure whenever property IsChecked changes in ViewModel a PropertyChanged event gets raised so that UI can listen to that and updates itself.

这篇关于DataTrigger的文本块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆