c# - WPF Databinding DataTrigger 根据 bool 值改变形状的颜色

标签 c# wpf data-binding datatrigger

我正在尝试通过数据绑定(bind)和数据触发器来更改形状的颜色。

但我仍然是 WPF 的新手。

我举个例子。这是一个分组框

<GroupBox x:Class="Server.Host.SingleAxisControls"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:host="clr-namespace:Server.Host"
         mc:Ignorable="d" 
         d:DesignWidth="200">
    <Grid>
        <StackPanel Orientation="Vertical" Width="180" >
            <host:MyRectangleControl x:Name="MyRectangle" />
            <Button Click="OnButton_Click" Width="80" Margin="20,5,20,5">On</Button>
            <Button Click="OffButton_Click" Width="80">Off</Button>
        </StackPanel>
    </Grid>
</GroupBox>

MyRectangleControl 是类似的用户控件

<UserControl x:Class="Server.Host.MyRectangleControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="30" d:DesignWidth="30">
<Grid>
    <Rectangle  HorizontalAlignment="Center"
              Height="25"
              Margin="0,0,0,0"
              Stroke="Black"
              VerticalAlignment="Center"
              Width="25" 
              Fill="red">
        <Rectangle.Style>
            <Style TargetType="Rectangle">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=Test,UpdateSourceTrigger=PropertyChanged}"
                                 Value="True">
                        <Setter Property="Fill"
                                Value="Green" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Rectangle.Style>
    </Rectangle>
</Grid>

在组框后面的代码中,我有类似的东西

namespace Server.Host
{
public partial class SingleAxisControls : INotifyPropertyChanged
{
    public SingleAxisControls()
    {
        InitializeComponent();

        MyRectangle.DataContext = this;
    }

    private bool _test;
    public bool Test
    {
        get { return _test; }
        set
        {
            _test = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("Test"));
            }
        }
    }

    private void OnButton_Click(object sender, RoutedEventArgs e)
    {
        Test = true;
    }

    private void OffButton_Click(object sender, RoutedEventArgs e)
    {
        Test = false;
    }
}

我不确定哪里出了问题,但是当我将 test 的值从 false 更改为 true 时,它​​似乎并没有改变矩形的颜色。

最佳答案

这是value precedence的问题.

当您直接在元素声明中设置 DependencyProperty 时,此值的优先级高于样式中设置的值。

您所要做的就是在样式中将 Fill 属性设置为红色:

<Rectangle  HorizontalAlignment="Center"
          Height="25"
          Margin="0,0,0,0"
          Stroke="Black"
          VerticalAlignment="Center"
          Width="25" 
          >
    <Rectangle.Style>
        <Style TargetType="Rectangle">
            <Setter Property="Fill" Value="Red"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=Test,UpdateSourceTrigger=PropertyChanged}"
                             Value="True">
                    <Setter Property="Fill"
                            Value="Green" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Rectangle.Style>
</Rectangle>

关于c# - WPF Databinding DataTrigger 根据 bool 值改变形状的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35875272/

相关文章:

C# windows 服务程序

c# - 在本地计算机上调试 Windows 应用商店应用程序

c# - Windows 本地文件系统访问 API

wpf - 将 WPF Datepicker 设置为 Null 时出现问题

wpf - 在 WPF XAML 中渲染 EMF/WMF 图像

c# - 显示数据绑定(bind) WPF 组合框的默认值

c# - 手机游戏全局记分牌

c# - WPF 中的 FlowLayout ItemsControl

javascript - 没有 $scope 的 AngularFire 3 路绑定(bind)

wpf - 如何 bool 值&& 两个可见性转换器