为什么隐藏扩展器的所有标头不起作用?

问题描述 投票:0回答:1

有两个按钮“ ShowAllExpanders”和“ HideAllExpanders”

展开所有标头效果很好,但是折叠所有标头仅需使用几个标头,我每次都必须按下“全部隐藏”按钮才能一一折叠。

如果我将Tag绑定到扩展器,那么我可以折叠所有扩展器,但是如果我只单击一个扩展器来折叠它,其余的全部都将被折叠。

[ParameterConfigViewList展开所有标题时,会给我所有标题(count = 17),但是当我按“ hide all”时,我只会看到两个(Count = 2)。

如果按下按钮“ HideAllExpanders”,我应该怎么做才能使每个扩展器折叠起来,也要使所有扩展器折叠起来?

Xaml代码是:

<ListView.GroupStyle> 
  <GroupStyle> 
    <GroupStyle.ContainerStyle> 
       <Style TargetType="{x:Type GroupItem}"> 
        <Setter Property="Template"> 
         <Setter.Value> 
          <ControlTemplate> 
           <Grid> 
             <Expander IsExpanded="True"> 
               <Expander.Style> 
                <Style TargetType="{x:Type Expander}"> 
                 <Setter Property="Visibility" Value="Visible" /> 
                 <Setter Property="Template"> 
                  <Setter.Value> 
                   <ControlTemplate TargetType="{x:Type Expander}"> 
                    <Border Background="{TemplateBinding Background}"> 
                     <DockPanel> 
                       <ToggleButton x:Name="HeaderSite" MinWidth="0" MinHeight="0" Padding=" 
                         {TemplateBinding Padding}" Content="{TemplateBinding Header}" 
                         ContentTemplate="{TemplateBinding HeaderTemplate}" 
                         ContentTemplateSelector="{TemplateBinding HeaderTemplateSelector}"  
                         DockPanel.Dock="Top" 
                         FocusVisualStyle="{DynamicResource ExpanderHeaderFocusVisual}"   
                         IsChecked="{Binding IsExpanded, Mode=TwoWay, RelativeSource={RelativeSource 
                          TemplatedParent}}" 
                         Style="{DynamicResource ExpanderDownHeaderStyle}" /> 
                         <ContentPresenter x:Name="ExpandSite" DockPanel.Dock="Bottom" Focusable="false" 
                         Visibility="Collapsed" /> 
                      </DockPanel> 
                     </Border> 
                    <ControlTemplate.Triggers> 
                     <Trigger Property="IsExpanded" Value="true"> 
                      <Setter TargetName="ExpandSite" Property="Visibility" Value="Visible" /> 
                      </Trigger> 
                     <Trigger Property="IsEnabled" Value="false"> 
                      <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" /> 
                     </Trigger> 
                    </ControlTemplate.Triggers> 
                   </ControlTemplate> 
                  </Setter.Value> 
                 </Setter> 
                </Style> 
               </Expander.Style> 
               <Expander.Header> 
                <StackPanel Orientation="Horizontal"> 
                 <TextBlock VerticalAlignment="Bottom" FontSize="14" Text="{Binding Name}" /> 
                 <TextBlock Margin="10,0,0,0" VerticalAlignment="Bottom" Text="{Binding ItemCount}" /> 
                 <TextBlock VerticalAlignment="Bottom" Text="configuration" /> 
                </StackPanel> 
              </Expander.Header> 
              <ItemsPresenter /> 
             </Expander> 
            </Grid> 
           </ControlTemplate> 
          </Setter.Value> 
         </Setter> 
        </Style> 
       </GroupStyle.ContainerStyle> 
      </GroupStyle> 
     </ListView.GroupStyle>

Xaml_code

View.xaml.cs中的代码:

private void HideAllExpanders_Click(object sender, RoutedEventArgs e)
{
  List<Expander> expander = GetVisualTree<Expander>(ParameterConfigViewList);
    for (int i = 0; i < expander.Count; i++)
      {
         expander[i].Height = 0;
         expander[i].Height = Double.NaN;
         expander[i].IsExpanded = false;
      }
}

private void ShowAllExpanders_Click(object sender, RoutedEventArgs e)
{
     List<Expander> expander = GetTreeObjects<Expander>(ParameterConfigViewList);
     expander.All(a => a.IsExpanded = true);
}

private List<T> GetTreeObjects<T>(DependencyObject obj) where T : DependencyObject
{
     List<T> objects = new List<T>();
     int count = VisualTreeHelper.GetChildrenCount(obj);
     for (int i = 0; i < count; i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(obj, i);
            if(child != null)
            {
               T requestedType = child as T;
                if(requestedType != null)
                    objects.Add(requestedType);
                    objects.AddRange(this.GetTreeObjects<T>(child));
            }
         }
     return objects;
}
c# wpf visual-studio listview expander
1个回答
0
投票
我该怎么做才能将每​​个扩展器都折叠起来,也要折叠起来全部都按下按钮“ HideAllExpanders”吗?
© www.soinside.com 2019 - 2024. All rights reserved.