sorton9999
sorton9999

Reputation: 182

Can I reuse a Style using different DataTrigger Bindings?

I am working on a WPF 4.0 window with many status indicators using Labels. In one example, the Labels turn GREEN and read "Available" if the bound property's enumeration Foo is AVAILABLE. The labels turn RED and read "Not Available" if the bound property is NOTAVAILABLE. I also have other labels that indicate status of different bound enum Bar, and they turn different colors and have different content based on this value.

I am able to successfully bind and turn one label's color and text based on the value of a bound property. I use a rather lengthy ControlTemplate in a DataTrigger's Setter just to change the label's text.

Here's what I have so far:

<Window> ...
    xmlns:cst="clr-namespace:CstCommonTypes;assembly=CstCommonTypes"
    ...
</Window>

<Label x:Name="Avail_Out_LBL" HorizontalAlignment="Left" Margin="111,44,0,0" VerticalAlignment="Top" Width="124" Height="18" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" SnapsToDevicePixels="False" Grid.Column="1" Padding="0">
  <Label.Style>
    <Style TargetType="{x:Type Label}">
        <Setter Property="Background" Value="#FFC08100"/>
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <Grid Background="{TemplateBinding Background}">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="24"/>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="24"/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="Degraded"
                                   Grid.Column="1"
                                   VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                   HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                   Padding="{TemplateBinding Padding}"
                                   Background="{TemplateBinding Background}"
                                   Foreground="{TemplateBinding Foreground}"
                                   Width="{TemplateBinding Width}"
                                   Height="{TemplateBinding Height}"
                             />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <DataTrigger Binding="{Binding MonitorAndControlData.Availability}" Value="{x:Static cst:Foo.Available}">
                <Setter Property="Background" Value="#FF567E4A"/>
                <Setter Property="Foreground" Value="White"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Grid Background="{TemplateBinding Background}">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="24"/>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="24"/>
                                </Grid.ColumnDefinitions>
                                <TextBlock Text="Available"
                                           Grid.Column="1"
                                           VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                           HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                           Padding="{TemplateBinding Padding}"
                                           Background="{TemplateBinding Background}"
                                           Foreground="{TemplateBinding Foreground}"
                                           Width="{TemplateBinding Width}"
                                           Height="{TemplateBinding Height}"
                                    />
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </DataTrigger>
            <DataTrigger Binding="{Binding MonitorAndControlData.Availability}" Value="{x:Static cst:Foo.NotAvailable}">
                <Setter Property="Background" Value="LightCoral"/>
                <Setter Property="Foreground" Value="White"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Grid Background="{TemplateBinding Background}">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="22"/>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="22"/>
                                </Grid.ColumnDefinitions>
                                <TextBlock Text=" Not Available"
                                           Grid.Column="1"
                                           VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                           HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                           Padding="{TemplateBinding Padding}"
                                           Background="{TemplateBinding Background}"
                                           Foreground="{TemplateBinding Foreground}"
                                           Width="{TemplateBinding Width}"
                                           Height="{TemplateBinding Height}"
                                    />
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Label.Style>

The above works, but I was wondering if I can reuse either the Style and give it different DataTrigger bindings or just reuse the ControlTemplate to cut down on xaml code. I tried to see if I could define a resource, but I couldn't figure out how to give it different Template Bindings for all the Labels.

Any help would be greatly appreciated.

Upvotes: 2

Views: 932

Answers (2)

brunnerh
brunnerh

Reputation: 184296

If you need more properties to bind to, then turn it into a custom control (i.e. a class that inherits from Control), then you can add additional dependency properties on said control, which you can then bind to in the template.

Upvotes: 1

mgarant
mgarant

Reputation: 585

It won't be possible to reuse the style if the bindings are not the same.

What you could do is make yourself a user control for your status indicators labels, Inside which you will create a Dependency Property (lets name it Availability) on which you will bind your Triggers to. But, you will have to make sure that you will always need the same type for your triggers. If the Availability binding is not of the same type for each of your labels, this solution won't work.

Here is a link on how to use Dependency Properties :

http://www.wpftutorial.net/dependencyproperties.html

Upvotes: 0

Related Questions