我正在尝试根据 Text 属性有条件地格式化文本块,但我无法让它工作:
<Style x:Key="StatusEnumTextStyle" TargetType="TextBlock" >
<Style.Triggers>
<DataTrigger Binding="{Binding Text}" Value="InProgress">
<Setter Property="Foreground" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
<TextBlock Text="InProgress" Style="{StaticResource StatusEnumTextStyle}"/>
但我收到一个绑定错误:
System.Windows.Data Error: 40 : BindingExpression path error: 'Text' property not found on 'object' ''NotifierViewModel' (HashCode=43600526)'. BindingExpression:Path=Text; DataItem='VM' (HashCode=43600526); target element is 'TextBlock' (Name=''); target property is 'NoTarget' (type 'Object')
好吧,我看到触发器试图获取 String.Text 但是如何将 TextBlock 的属性用于触发器?
背景:基本上我想要一个基于名为“StatusEnum”的枚举的条件样式,但不依赖于不断变化的变量名称,因为如果一个人期望一个名为“CurrentStatusEnum”的变量,我需要两个相似的样式,如果var名称是另一个不同,例如“NewStatusEnum”。诉诸匹配“文本”之类的属性将允许我在使用 TextBlock 时使用相同的样式