简体   繁体   中英

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

I have a property HashCodeValue . Based on HashCode in the HashCodeValue property I need to change the visibility of the Control using a DataTrigger.

The WPF XAML source code:

<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. Out of three buttons, the property holds any one of the buttons' HashCode at run time. Based on the value, I want to change the Visibility of the corresponding button to Visible .

Kindly assist me how to write the DataTrigger for my scenario.

Since Hashcode value is known only at runtime, i think, you should use IValueConverter to reach your goal. Basic idea is that you have hash as converter value, some additional string as converter parameter, getting bool as converter output.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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