WPF Trigger and Binding problem
I am creating a button with a simple Polyline which I want to change color when the button is Disabled (btnUp.IsEnabled = false
).
I tried this:
<RepeatButton x:Name="btnUp" Foreground="Green">
<RepeatButton.Style>
<开发者_如何学Go;Style TargetType="{x:Type RepeatButton}">
<Setter Property="Content">
<Setter.Value>
<Polyline x:Name="arrowUp" Points="0,2 3,0 6,2" Stroke="{Binding RelativeSource={RelativeSource AncestorType=RepeatButton}, Path=Foreground, Mode=TwoWay}" StrokeThickness="2"/>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="#FFFFFFFF"/>
</Trigger>
</Style.Triggers>
</Style>
</RepeatButton.Style>
</RepeatButton>
But the Polyline still has the same (Green) color when the button is disabled. Even though I did expect it to be white because of the databinding between the button.foreground and polyline.stroke.
However if I change the trigger to this it works (the button is collapsed):
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Visibility" Value="Collapsed"/>
</Trigger>
I found the answer on my own.
I moved the Foreground="Green"
to a separate setter, <Setter Property="Foreground" Value="#FF383838"/>
inside the <Style>
, then it worked.
I'm not sure why though.
Here's the complete solution:
<RepeatButton x:Name="btnUp">
<RepeatButton.Style>
<Style TargetType="{x:Type RepeatButton}">
<Setter Property="Foreground" Value="Green"/>
<Setter Property="Content">
<Setter.Value>
<Polyline x:Name="arrowUp" Points="0,2 3,0 6,2" Stroke="{Binding RelativeSource={RelativeSource AncestorType=RepeatButton}, Path=Foreground, Mode=TwoWay}" StrokeThickness="2"/>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="#FFFFFFFF"/>
</Trigger>
</Style.Triggers>
</Style>
</RepeatButton.Style>
</RepeatButton>
精彩评论