WPF:如何使用DataTrigger设置TextBlock的Foreground属性

时间:2022-08-25 14:17:53

This is my XAML:

这是我的XAML:

<TextBlock Name="SeverityText"
           Grid.Column="1"
           Grid.Row="0"
           Foreground="Red">
    <TextBlock.Triggers>

        <DataTrigger Binding="{Binding Path=Severity}">
            <DataTrigger.Value>
                <sm:Severity>Warning</sm:Severity>
            </DataTrigger.Value>
            <Setter TargetName="SeverityText"
                    Property="Foreground"
                    Value="Yellow" />
        </DataTrigger>
                 <DataTrigger Binding="{Binding Path=Severity}">
            <DataTrigger.Value>
                <sm:Severity>Information</sm:Severity>
            </DataTrigger.Value>
            <Setter TargetName="SeverityText"
                    Property="Foreground"
                    Value="White" />
        </DataTrigger>


    </TextBlock.Triggers>
    <TextBlock>Severity:</TextBlock>
    <TextBlock Text="{Binding Path=Severity}" />
</TextBlock>

This is my error message:

这是我的错误消息:

Cannot find the static member 'ForegroundProperty' on the type 'ContentPresenter'.

在“ContentPresenter”类型上找不到静态成员“ForegroundProperty”。

sm:Severity is an enumeration I imported.

sm:严重性是我导入的枚举。

2 个解决方案

#1


10  

Your triggers and setters need to be defined in a style, rather than on the TextBlock directly:

您的触发器和设置器需要以样式定义,而不是直接在TextBlock上定义:

<TextBlock>
    <TextBlock.Style>
        <Style TargetType="TextBlock">
            <Style.Triggers>

               <DataTrigger Binding="{Binding Severity}"> 
                   <DataTrigger.Value> 
                       <sm:Severity>Warning</sm:Severity> 
                   </DataTrigger.Value> 
                   <Setter TargetName="SeverityText" 
                           Property="Foreground" 
                           Value="Yellow" /> 
               </DataTrigger>

            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

#2


3  

Writing the full path of the property also works:
So

编写属性的完整路径也有效:所以

Property="Foreground" -> Property="TextBlock.Foreground"

Property =“Foreground” - > Property =“TextBlock.Foreground”

However as suggested in the previous answer, you get:

但是,如上一个答案所示,您会得到:

System.InvalidOperationException: Triggers collection members must be of type EventTrigger.

System.InvalidOperationException:触发器集合成员必须是EventTrigger类型。


...if you don't put it in a style.

...如果你不把它放在一个风格。

#1


10  

Your triggers and setters need to be defined in a style, rather than on the TextBlock directly:

您的触发器和设置器需要以样式定义,而不是直接在TextBlock上定义:

<TextBlock>
    <TextBlock.Style>
        <Style TargetType="TextBlock">
            <Style.Triggers>

               <DataTrigger Binding="{Binding Severity}"> 
                   <DataTrigger.Value> 
                       <sm:Severity>Warning</sm:Severity> 
                   </DataTrigger.Value> 
                   <Setter TargetName="SeverityText" 
                           Property="Foreground" 
                           Value="Yellow" /> 
               </DataTrigger>

            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

#2


3  

Writing the full path of the property also works:
So

编写属性的完整路径也有效:所以

Property="Foreground" -> Property="TextBlock.Foreground"

Property =“Foreground” - > Property =“TextBlock.Foreground”

However as suggested in the previous answer, you get:

但是,如上一个答案所示,您会得到:

System.InvalidOperationException: Triggers collection members must be of type EventTrigger.

System.InvalidOperationException:触发器集合成员必须是EventTrigger类型。


...if you don't put it in a style.

...如果你不把它放在一个风格。