How to Work With ControlTemplate.Triggers ,ControlTemplate.TargetType and accessing controls within ControlTemplate Using Keyboard ShortCuts

StackOverflow https://stackoverflow.com/questions/17474917

سؤال

I have an usercontrol with a LlistBox, which has an ItemsSource with a Collectionview. In order to group, I used the group style where the grouped items will be within the Expander control as follows.

I want the Exapander with the label content "UNKNOWN" has to be Closed by default and rest of them should remain opened, so i have tried two methods , but none of them worked,

  1. Using Data Trigger Within Controltemplate.Triggers
  2. Using Data Trigger Within Controltemplate.Resources

Since the number of Expander counts varies based on the group ,Is it possible to open and close the Expander using ShortCut keys??

for eg:If i have two Expander(Animals/Birds),using Alt+F1 i need to close or open " Exapnder Animals"

using Alt+F2 i need to close or open " Exapnder Birds"

<GroupStyle.ContainerStyle>
    <Style TargetType="{x:Type GroupItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type GroupItem}">




                    <ControlTemplate.Triggers>
                        <DataTrigger Binding="{Binding ElementName=lbl, Path=Content}" Value="UNKNOWN">
                            <Setter Property="Expander.IsExpanded" Value="False" />
                        </DataTrigger>
                    </ControlTemplate.Triggers>



                    <ControlTemplate.Resources>
                        <Style TargetType="{x:Type Expander}">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding ElementName=lbl, Path=Content}" Value="UNKNOWN">
                                    <Setter Property="Expander.IsExpanded" Value="False" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                   </ControlTemplate.Resources>



                   <Expander IsExpanded="True" FlowDirection="LeftToRight"    >
                       <Expander.Header >
                           <Label x:Name="lbl"  BorderBrush="Black"  BorderThickness="0.5"  
                                  Content="{Binding Path = Name}"  Width="{Binding ElementName=MyList, Path=ActualWidth}"
                                  FontSize="16" FontFamily="Verdana"/>
                        </Expander.Header>                                   
                        <Expander.Content>
                            <ItemsPresenter />
                        </Expander.Content>
                    </Expander>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</GroupStyle.ContainerStyle>
هل كانت مفيدة؟

المحلول

Modify your Datatrigger like this:

<ControlTemplate.Triggers>
   <DataTrigger Binding="{Binding ElementName=lbl, Path=Content}" Value="UNKNOWN">
      <Setter TargetName="exp" Property="IsExpanded" Value="False" />
   </DataTrigger>
</ControlTemplate.Triggers>

and name the expander:

<Expander x:Name="exp" IsExpanded="True" FlowDirection="LeftToRight">
...

Note: the expander must be defined before the trigger:

<ControlTemplate TargetType="{x:Type GroupItem}">    
   <Expander ...
   <ControlTemplate.Triggers> ...
</ControlTemplate>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top