How do I bind a datatrigger in xaml to a code-defined dependency property?
Asked Answered
H

2

5

My code behind for a window defines a dependency property, "Active"...

public partial class MainWindow : Window
{
  public MainWindow() { InitializeComponent(); }

  public bool Active
  {
     get { return (bool) GetValue(ActiveProperty); }
     set { SetValue(ActiveProperty, value); }
  }
  public static readonly DependencyProperty ActiveProperty =
      DependencyProperty.Register("Active", typeof(bool), typeof(MainWindow), new UIPropertyMetadata(false));
}

And then I bind to that property using two checkboxes in xaml. I also want to change the fill of a rectangle based on that property. How can I make this work?

<Window x:Class="WpfTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        Height="350"
        Width="525"
        DataContext="{Binding RelativeSource={RelativeSource Self}}">
  <StackPanel>
    <CheckBox IsChecked="{Binding Active}" />
    <CheckBox IsChecked="{Binding Active}" />
    <Rectangle Fill="Gray"
               Width="50"
               Height="50">
      <Rectangle.Style>
        <Style TargetType="Rectangle">
          <Style.Triggers>
            <DataTrigger Binding="{Binding Active}"
                         Value="True">
              <Setter Property="Fill"
                      Value="Green" />
            </DataTrigger>
          </Style.Triggers>
        </Style>
      </Rectangle.Style>
    </Rectangle>
  </StackPanel>
</Window>

Checking one box auto-checks the other, but does not change the rectangle color :(

Henceforth answered 19/4, 2013 at 23:31 Comment(0)
L
5

Locally set properties always override style set properties, so you need to remove the locally set one and set the default value in your style instead:

<Rectangle Width="50" Height="50">
    <Rectangle.Style>
        <Style TargetType="Rectangle">
            <Setter Property="Fill" Value="Gray" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding Active}" Value="True">
                    <Setter Property="Fill" Value="Green"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Rectangle.Style>
</Rectangle>
Laurettalaurette answered 21/4, 2013 at 12:58 Comment(0)
D
3

Once you set a property, the triggers defiened on this property by the style won't work anymore. You can define another dataTrigger to change the background on False value :

  <StackPanel>
        <CheckBox IsChecked="{Binding Active}"/>
        <CheckBox IsChecked="{Binding Active}"/>
        <Rectangle Width="50" Height="50">
            <Rectangle.Style>
                <Style TargetType="Rectangle">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Active}" Value="True">
                            <Setter Property="Fill" Value="Green"/>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding Active}" Value="false">
                            <Setter Property="Fill" Value="Gray"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Rectangle.Style>
        </Rectangle>
    </StackPanel>
Dispose answered 21/4, 2013 at 12:50 Comment(1)
I accepted the other answer for being faster and more concise syntax, but this answer is equally effective, so please accept my humble +1.Henceforth

© 2022 - 2024 — McMap. All rights reserved.