C# 如何获得触发器以根据 DataContext 属性更改 TextBlock 的颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/914806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:05:28  来源:igfitidea点击:

How can I get a trigger to change the color of a TextBlock based on a DataContext Property?

c#wpfxamltriggersstyles

提问by Edward Tanguay

Why does the following code get the runtime error:

为什么以下代码会出现运行时错误:

Members of the Triggers collection must be of type EventTrigger

Triggers 集合的成员必须是 EventTrigger 类型

But the EventTrigger element doesn't have a Binding property.

但是 EventTrigger 元素没有 Binding 属性。

So how do I change the color of the TextBlock based on the DataContext Property?

那么如何根据 DataContext 属性更改 TextBlock 的颜色呢?

XAML:

XAML:

<Window x:Class="TestTrigger123345.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <StackPanel HorizontalAlignment="Left">
        <TextBlock Text="{Binding Status}">
            <TextBlock.Triggers>
                <DataTrigger Binding="{Binding Status}" Value="off">
                    <Setter Property="TextBlock.Background" Value="Red"/>
                </DataTrigger>
            </TextBlock.Triggers>
        </TextBlock>
    </StackPanel>
</Window>

Code:

代码:

namespace TestTriggers
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            DataContext = this;
            Status = "off";
        }

        public string Status { get; set; }    
    }
}

采纳答案by Arcturus

That is because you can only set event triggers directly on the Trigger property..

那是因为您只能直接在 Trigger 属性上设置事件触发器。

Use a style to achieve what you want:

使用样式来实现您想要的:

<Style x:Key="Triggers" TargetType="TextBlock">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Status}" Value="off">
            <Setter Property="TextBlock.Background" Value="Red"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

The following objects have Triggers collections that can contain the trigger types listed:

以下对象具有可以包含列出的触发器类型的 Triggers 集合:

FrameworkElement     Style, ControlTemplate, DataTemplate
----------------     ------------------------------------
EventTrigger         EventTrigger
                     Trigger or MultiTrigger
                     DataTrigger or MultiDataTrigger

回答by Matt Hamilton

You can do it in a style:

你可以用一种风格来做:

<TextBlock Text="{Binding Status}">
    <TextBlock.Style>
        <Style TargetType="{x:Type TextBlock}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Status}" Value="off">
                    <Setter Property="Background" Value="Red"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

回答by paparazzo

There is a typo as you did not close out Style.Triggers. And I found I needed to use the property TextBlock.Background. Thanks, you got me to the solution.

有一个错字,因为您没有关闭 Style.Triggers。我发现我需要使用属性 TextBlock.Background。谢谢,你让我找到了解决方案。

    <TextBlock.Style>
        <Style TargetType="{x:Type TextBlock}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Status}" Value="off">
                    <Setter Property="TextBlock.Background" Value="Red"/>
                 </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>