Textblock DataTriggers未触发

时间:2013-01-17 15:51:38

标签: c# wpf xaml datacontext textblock

我正在尝试将一些数据绑定到TextBlock。 TextBlock用于显示滑块条值,当滑块条值更改时,我希望TextBlock文本将颜色更改为红色。

我的XAML是这样的:

<Grid Height="227">
    <TextBlock Margin="114,60,112,150" Name="textBlock1" Text="{Binding Path=DispVal}" Width="42" Grid.Column="1" HorizontalAlignment="Center" TextAlignment="Center" FontWeight="Bold">
        <TextBlock.Style>
            <Style TargetType="{x:Type TextBlock}"> 
                <Setter Property="Foreground" Value="Black"/>

                <Style.Triggers> 
                    <DataTrigger Binding="{Binding Path=IsChanged}" Value="true"> 
                        <Setter Property="TextBlock.Foreground" Value="Red" /> 
                    </DataTrigger> 
                    <DataTrigger Binding="{Binding Path=IsChanged}" Value="false"> 
                        <Setter Property="TextBlock.Foreground" Value="Black" /> 
                    </DataTrigger> 
                </Style.Triggers> 
            </Style> 
        </TextBlock.Style>            
    </TextBlock> 
</Grid>

我使用DataContext绑定了TextBlock:

texBlock1.DataContext = m_slider;

当我的滑块更新处理程序触发时,我更新了m_slider对象。

但是,我没有任何文字或颜色变化。

1 个答案:

答案 0 :(得分:0)

您可以使用文本框替换textblock,并通过应用以下属性使其外观相似

<Setter Property="IsReadOnly" Value="True"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderThickness" Value="0"/>

然后,您可以使用TextChanged事件,以便在绑定更改文本时更新其样式

相关问题