简体   繁体   English

如何根据WPF中控件的哈希码编写DataTrigger

[英]How to write a DataTrigger based on hashcode of a Control in WPF

I have a property HashCodeValue .我有一个属性HashCodeValue Based on HashCode in the HashCodeValue property I need to change the visibility of the Control using a DataTrigger.基于 HashCodeValue 属性中的 HashCode,我需要使用 DataTrigger 更改控件的可见性。

The WPF XAML source code: WPF XAML 源代码:

<Style TargetType="{x:Type DataGridColumnHeader}" x:Key="DummyFilterDataGridColumnHeader">
   <Setter Property="Template">
      <Setter.Value>
          <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
              <Button Content="Super I" Visibility="Collapsed" />
              <Button Content="Super II" Visibility="Collapsed" />
              <Button Content="Super III" Visibility="Collapsed" />
              <ControlTemplate.Triggers>
                    <DataTrigger Property="{Binding HashCodeValue}" Value="???">
                        <Setter TargetName="Button" Property="Visibility" Value="Visible" />
                    </DataTrigger>
              </ControlTemplate.Triggers>
          </ControlTemplate>
      </Setter.Value>
   </Setter>
</Style>

We can't know the HashCode at compile time, it is generated only on at run time.我们无法在编译时知道 HashCode,它仅在运行时生成。 Out of three buttons, the property holds any one of the buttons' HashCode at run time.在三个按钮中,该属性在运行时保存任何一个按钮的 HashCode。 Based on the value, I want to change the Visibility of the corresponding button to Visible .根据该值,我想将相应按钮的 Visibility 更改为Visible

Kindly assist me how to write the DataTrigger for my scenario.请帮助我如何为我的场景编写 DataTrigger。

Since Hashcode value is known only at runtime, i think, you should use IValueConverter to reach your goal.由于 Hashcode 值仅在运行时已知,我认为您应该使用 IValueConverter 来实现您的目标。 Basic idea is that you have hash as converter value, some additional string as converter parameter, getting bool as converter output.基本思想是你有哈希作为转换器值,一些额外的字符串作为转换器参数,得到 bool 作为转换器输出。

So, your code might look like this:因此,您的代码可能如下所示:

 <ControlTemplate.Triggers>
                    <DataTrigger Property="{Binding HashCodeValue,Converter={StaticResource myConverter,ConverterParameter=myparameter}}" Value="true">
                        <Setter TargetName="Button" Property="Visibility" Value="Visible" />
                    </DataTrigger>
              </ControlTemplate.Triggers>

This trick will give you what you need, if i understood you correctly.如果我理解正确的话,这个技巧会给你你需要的东西。

You have to put the Hashcodevalue in the Value in DataTrigger which you bind.你必须把Hashcodevalue的ValueDataTrigger你绑定。

 <Style TargetType="{x:Type DataGridColumnHeader}" x:Key="DummyFilterDataGridColumnHeader">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
                        <StackPanel Name="ButtonsPanel">
                            <Button Name="BtnSuper1" Content="Super I" Visibility="Collapsed">
                                <Button.Style>
                                    <Style TargetType="Button">
                                        <Setter Property="Visibility" Value="Collapsed"/>
                                    </Style>
                                </Button.Style>
                            </Button>
                            <Button Name="BtnSuper2" Content="Super II" Visibility="Collapsed">
                                <Button.Style>
                                    <Style TargetType="Button">
                                        <Setter Property="Visibility" Value="Collapsed"/>
                                    </Style>
                                </Button.Style>
                            </Button>
                            <Button  Name="BtnSuper3" Content="Super III" Visibility="Collapsed" >
                                <Button.Style>
                                    <Style TargetType="Button">
                                        <Setter Property="Visibility" Value="Collapsed"/>
                                    </Style>
                                </Button.Style>
                            </Button>
                        </StackPanel>
                        <ControlTemplate.Triggers>
                            <DataTrigger Binding="{Binding HashCodeValue}" Value="Give Your HashCode for matching ">
                                <Setter TargetName="BtnSuper1" Property="Visibility" Value="Visible" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding HashCodeValue}" Value="Give Your HashCode for matching">
                                <Setter TargetName="BtnSuper2" Property="Visibility" Value="Visible" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding HashCodeValue}" Value="Give Your HashCode for matching ">
                                <Setter TargetName="BtnSuper3" Property="Visibility" Value="Visible" />
                            </DataTrigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM