tags:

views:

129

answers:

1

In WPF is there some mechanisim to reuse the property setters amoung differant triggers? For example I have the following.

                <Style TargetType="{x:Type Label}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding}" Value="{x:Null}">
                        <Setter Property="Content" Value="Not Connected" />
                        <Setter Property="Foreground" Value="Red" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding IsConnected}" Value="False">
                        <Setter Property="Content" Value="Not Connected" />
                        <Setter Property="Foreground" Value="Red" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding IsConnected}" Value="True">
                        <Setter Property="Content" Value="Connected" />
                        <Setter Property="Foreground" Value="Green" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>

The setters for Null and False are identical and it would be nice if there was only a single point of change.

+1  A: 

You can always create a resource like this:

<Setter x:Key="setter1" Property="Content" Value="Not Connected" />

However, you cannot use resource keys as object in a collection.

You could set somewhere

<SomeObject Setter="{StaticResource setter1}"/>

but Setters are almost always added to collections and you cannot add resource keys in xaml collection syntax.

The only scenario I can think of which would support this would be to create a SetterBaseCollection resource with those two identical pairs of Setters,

<SetterBaseCollection x:Key="settersKey">
    <Setter Property="Label.Content" Value="Not Connected" />
    <Setter Property="Label.Foreground" Value="Red" />
</SetterBaseCollection>

<Style TargetType="{x:Type Label}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding}" Value="{x:Null}" Setters="{StaticResource settersKey}"/>

    // ...

    </Style.Triggers>
</Style>

but the DataTrigger.Setters collection property is readonly and cannot be set from xaml.

So, the answer would be no.

kek444