Updating the foreground of a label on window active property-WPF
I have a l开发者_如何学Cabel which shows the name of the window. I want to update the colour of the label on the IsActive property of the window using styles and triggers so that all the labels inheriting this style should exhibit the same property. Please can anyone suggest me how?
I tried like this:
<Style TargetType="{x:Type Label}" x:Key="HeaderLabel">
<Style.Triggers>
<DataTrigger Binding="{Binding (Window.IsActive)}" Value="True">
<Setter Property="FontSize" Value="15"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
</DataTrigger>
<DataTrigger Binding="{Binding (Window.IsActive)}" Value="False">
<Setter Property="FontSize" Value="15"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
</DataTrigger>
</Style.Triggers>
</Style>
Try this binding in DataTrigger:
Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=IsActive}"
By label which shows the name of the window you mean the one on the window titlebar? Or something else?
If it's the latter, then you could set default style of the label and use only one trigger for inactive state. Also make sure you have a Window
in label's datacontext. It should go like this (didn't check it):
<Style TargetType="{x:Type Label}" x:Key="HeaderLabel">
<Setter Property="FontSize" Value="15"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Style.Triggers>
<DataTrigger Binding="{Binding (Window.IsActive)}" Value="False">
<Setter Property="FontSize" Value="15"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
</DataTrigger>
</Style.Triggers>
</Style>
If you want to change titlebar, I think the easiest way would be to override Window style completely (for all windows themes).
精彩评论