如何在WPF XAML中将Control的HashCode作为CommandParameter传递

时间:2016-02-05 10:56:57

标签: c# wpf xaml hashcode

我有一个DataGridColumnHeader Sytle,因为我有一个过滤按钮。我需要将Button HashCode作为命令参数传递

<Style TargetType="{x:Type DataGridColumnHeader}" x:Key="DummyFilterDataGridColumnHeader">
   <Setter Property="Template">
      <Setter.Value>
          <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
              <Button Command="{Binding Path=DataContext.FilterPopUpCommand, 
                                  RelativeSource={RelativeSource Mode=FindAncestor, 
                                                  AncestorType={x:Type Window}}}">
                  <Button.CommandParameter>
                      <MultiBinding Converter="{StaticResource MultiValueConverterKey}">
                          <Binding RelativeSource="{ RelativeSource Mode=FindAncestor, 
                                                     AncestorType={x:Type cust:DataGrid}}" />
                          <Binding Path="Column" 
                                   RelativeSource="{RelativeSource Mode=TemplatedParent}" />
                          <Binding Path="HashCode"  />
                      </MultiBinding>
                  </Button.CommandParameter>
              </Button>
          </ControlTemplate>
      </Setter.Value>
   </Setter>
</Style>
  

注意:我的要求是仅从不在的XAML中传递HashCode   从按钮控件中提取C#。

1 个答案:

答案 0 :(得分:1)

由于获取HashCode的{​​{1}}的唯一方法是通过Button方法,我建议您将Button本身的引用传递给命令,如下所示:< / p>

GetHashCode()

<ControlTemplate TargetType="{x:Type DataGridColumnHeader}"> <Button x:Name="Btn" Command="{Binding Path=DataContext.FilterPopUpCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"> <Button.CommandParameter> <MultiBinding Converter="{StaticResource MultiValueConverterKey}"> <Binding RelativeSource="{ RelativeSource Mode=FindAncestor, AncestorType={x:Type cust:DataGrid}}" /> <Binding Path="Column" RelativeSource="{RelativeSource Mode=TemplatedParent}" /> <Binding ElementName="Btn" /> </MultiBinding> </Button.CommandParameter> </Button> </ControlTemplate> 中,提取哈希代码并将其传递给命令:

MultivalueConverter