I have three buttons that differ only in the text displayed, the parameter they pass to the command and a value compared in a trigger for the image. Is there a way to coalesce this into a single template to eliminate all the duplicate XAML?
Style Definition:
<Style TargetType="{x:Type Button}" x:Key="myGridHeaderButton">
<Setter Property="Background" Value="Blue" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="Foreground" Value="White" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}" BorderThickness="0,0,1,0" BorderBrush="white">
<ContentPresenter Margin="1" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="LightBlue" />
</Trigger>
</Style.Triggers>
</Style>
Button Declaration:
<Button Grid.Column="2" Style="{StaticResource myGridHeaderButton}" Command="{Binding Path=ColumnHeaderClickCommand}" CommandParameter="Description">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="1" />
<ColumnDefinition Width="20" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="Custodian" Margin="3,0,0,0" />
<Image Grid.Column="2" Width="16" Height="16">
<Image.Style>
<Style TargetType="{x:Type Image}">
<Setter Property="Source" Value="/Resources/Sort.Unsorted.png" />
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=SortAscending}" Value="True" />
<Condition Binding="{Binding Path=SortColumn}" Value="Description" />
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="Source" Value="/Resources/Sort.Ascending.png" />
</MultiDataTrigger.Setters>
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=SortAscending}" Value="False" />
<Condition Binding="{Binding Path=SortColumn}" Value="Description" />
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="Source" Value="/Resources/Sort.Descending.png" />
</MultiDataTrigger.Setters>
</MultiDataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
</Grid>
</Button>
I figure the Command and CommandParameter would be declared just as they are now. If it was simply a difference in text, I think just placing the <ContentPresenter />
in the right place would work, but there is a style embedded in the image that has a couple of triggers based on different values and I can't figure out how to move all that stuff to the template and still be able to define the differing trigger values.
In this case, the trigger values in question always match the CommandParameter value... Would I somehow bind to that?
Thanks.
J