1

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

Digital Camel
  • 175
  • 2
  • 14
  • 1
    You could write a couple of attached properties for the additional values, and do templatebindings to get to them from inside the template. A button subclass with additional dependency properties is another option. – 15ee8f99-57ff-4f92-890c-b56153 May 27 '17 at 01:18
  • @EdPlunkett I will have to do a bit of research on creating attached properties, etc. But how would you reference that in the trigger condition? Using a Binding or a TemplateBinding in the Value of a Condition doesn't seem to work. The compiler complains that a binding isn't valid, there. Could you provide an example of what you're suggesting? – Digital Camel May 27 '17 at 03:16
  • Might have to write a multi value converter in that case. – 15ee8f99-57ff-4f92-890c-b56153 May 27 '17 at 03:17
  • Perhaps answers to [this question](https://stackoverflow.com/q/19398450/199364) are relevant. – ToolmakerSteve Mar 17 '19 at 09:10

0 Answers0