tags:

views:

62

answers:

2

I have a label 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>
A: 

Hi Deb!

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).

Anvaka
hey ,Actually the default windows title bar in set to inactive in my application. the title is being showed by a label. I want to change the colour of the label wen the window is active or inactive.
Deb
A: 

Try this binding in DataTrigger:

Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=IsActive}"
Veer