I'm trying to set the color of a rectangle (WPF) with triggers, depending on a boolean DependencyProperty, which I am binding to the Tag property of the rectangle.
I have the following code:
public partial class MainWindow : Window
{
public Boolean isAutoStart
{
get { return (Boolean)GetValue(isAutoStartProperty); }
set { SetValue(isAutoStartProperty, value); }
}
public static readonly DependencyProperty isAutoStartProperty =
DependencyProperty.Register("isAutoStart", typeof(Boolean),
typeof(MainWindow), new PropertyMetadata(true));
private void Window_Loaded(object sender, RoutedEventArgs e)
{
isAutoStart = false;
}
}
and in XAML:
<Window.Resources>
<Style x:Key="TriggerDark" TargetType="Rectangle">
<Setter Property="Fill" Value="Green" />
<Style.Triggers>
<Trigger Property="Tag" Value="False">
<Setter Property="Fill" Value="Red" />
</Trigger>
<Trigger Property="Tag" Value="True">
<Setter Property="Fill" Value="Green" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Rectangle Style="{StaticResource ResourceKey=TriggerDark}" Tag="{Binding Path=isAutoStart, UpdateSourceTrigger=PropertyChanged}">
If I hardcode "True" or "False" into the tag property of the rectangle, the triggers work correctly. And if I print the value of the tag property on runtime to console, the binding works, but the triggers do not fire.
Any ideas what I am doing wrong?
Thanks!