这是我的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>


这是我的错误信息:


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


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

最佳答案

您的触发器和设置器需要以一种样式定义,而不是直接在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>

关于wpf - WPF:如何使用DataTrigger设置TextBlock的Foreground属性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2144526/

10-15 02:45