WPF数据绑定DataTrigger根据布尔值

时间:2016-03-08 18:34:39

标签: c# wpf data-binding datatrigger

我试图通过数据绑定和数据触发来改变形状的颜色。

但我仍然是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>

在groupbox背后的代码中,我有类似

的内容
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时,它似乎没有改变矩形的颜色。

任何建议都将不胜感激。

2 个答案:

答案 0 :(得分:7)

这是value precedence的问题。

当您在Element的声明中直接设置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>

答案 1 :(得分:0)

另一个有效选项是将转换器写入绑定。它看起来像这样:

class BooleanToBrushConverter : IValueConverter
    {
        public object Convert(object value, Type targetType,  System.Globalization.CultureInfo culture)
        {
            if ((bool)value)
            {
                return new SolidColorBrush(Colors.Black);
            }
            return new SolidColorBrush(Colors.LightGray);
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

(您可以将颜色设置为您想要的任何颜色) 而Xaml看起来像这样:

<Rectangle  HorizontalAlignment="Center"
              Height="25"
              Margin="0,0,0,0"
              Stroke="Black"
              VerticalAlignment="Center"
              Width="25" 
              Fill="{Binding Test, Converter={StaticResource b2b}}">

将其放在xaml的顶部:

<UserControl.Resources>
    <BooleanToBrushConverter x:Key="b2b" />
</UserControl.Resources>

注意:如果您的转换器位于不同的位置,您需要将其包含在命名空间中,并在声明前加上您命名的命名空间,即

xlmns:converters="clr-namespace:Project.Converters"
<UserControl>标记中的

然后在资源中<converters: BooleanToBrushConverter x:Key="b2b" />