如何在wpf模板中允许变量?

时间:2017-05-26 22:46:14

标签: wpf triggers controltemplate

我有三个按钮,仅在显示的文本,传递给命令的参数和图像触发器中的值进行比较时有所不同。有没有办法将其合并为一个模板以消除所有重复的XAML?

样式定义:

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

我认为Command和CommandParameter将像现在一样被声明。如果它只是文本上的差异,我认为将<ContentPresenter />放在正确的位置会起作用,但是图像中嵌入了一种样式,它有几个基于不同值的触发器,我可以&#39 ; t弄清楚如何将所有内容移动到模板中,并且仍然能够定义不同的触发值。

在这种情况下,有问题的触发器值始终与CommandParameter值匹配...我会以某种方式绑定到那个吗?

感谢。

Ĵ

0 个答案:

没有答案