【发布时间】:2019-11-27 21:17:09
【问题描述】:
有两个按钮“ShowAllExpanders”和“HideAllExpanders”
展开所有标题都可以正常工作,但折叠所有标题只需要一些标题,我每次都必须按下“全部隐藏”按钮才能一一折叠。
如果我将标签绑定到扩展器,那么我可以折叠所有扩展器,但如果我只单击一个扩展器来折叠它,那么其余的都会折叠。
展开所有标题时,ParameterConfigViewList 给了我所有标题(count =17),但是当我按下“全部隐藏”时,我只给我两个(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>
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;
}
【问题讨论】:
-
请使用代码的 XAML 部分编辑您的问题,而不是添加图片
-
我已经添加了你想要的 xaml 代码