我有一个充满了一些物品的ListView
。这些项目有两个属性,ItemName
和ItemGroup
,我想用它们的第二个属性对它们进行分组。所以我写了这样的东西:
<Grid>
<Grid.Resources>
<Style x:Key="groupStyle" TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Expander IsExpanded="False" Header="{Binding Name}">
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<ListView x:Name="lv">
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding ItemName}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
<ListView.GroupStyle>
<GroupStyle ContainerStyle="{StaticResource groupStyle}"/>
</ListView.GroupStyle>
</ListView>
</Grid>
并在后面的代码中
// here, list is the collection of items with mentioned properties.
lv.ItemsSource = list;
var view = (CollectionView) CollectionViewSource.GetDefaultView(lv.ItemsSource);
if (view.GroupDescriptions != null)
{
view.GroupDescriptions.Clear();
view.GroupDescriptions.Add(new PropertyGroupDescription("ItemGroup"));
}
现在一切顺利。但问题是,有时我想扩展代码背后的所有Expander
s,我发现无法访问它们并将其IsExpanded
属性设置为true。我怎样才能做到这一点?
编辑:这是我用来查找扩展器的方法,例如FindChildren<Expander>(lv)
但它总是返回一个空集合
public static IEnumerable<T> FindChildren<T>(DependencyObject ui) where T : DependencyObject
{
if (ui == null) yield break;
int childcount = ui is Visual ? VisualTreeHelper.GetChildrenCount(ui) : 0;
if (childcount == 0)
{
var children = LogicalTreeHelper.GetChildren(ui).OfType<DependencyObject>();
foreach (T descendant in children.SelectMany(FindChildren<T>))
yield return descendant;
}
for (int n = 0; n < childcount; n++)
{
var child = VisualTreeHelper.GetChild(ui, n);
if (child is T)
{
yield return (T) child;
continue;
}
foreach (T descendant in FindChildren<T>(child))
yield return descendant;
}
}
您可以使用递归方法和Expander
类在可视化树中找到VisualTreeHelper
元素:
private void ExpandButton_Click(object sender, RoutedEventArgs e)
{
foreach (Expander gi in FindVisualChildren<Expander>(lv))
{
gi.IsExpanded = true;
}
}
private static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T)
{
yield return (T)child;
}
foreach (T childOfChild in FindVisualChildren<T>(child))
{
yield return childOfChild;
}
}
}
}
编辑:如果您想在设置ItemsSource
的ListView
属性后立即执行此操作,则需要等到容器已创建。处理Loaded
事件:
public MainWindow()
{
InitializeComponent();
// here, list is the collection of items with mentioned properties.
lv.ItemsSource = list;
var view = (CollectionView)CollectionViewSource.GetDefaultView(lv.ItemsSource);
if (view.GroupDescriptions != null)
{
view.GroupDescriptions.Clear();
view.GroupDescriptions.Add(new PropertyGroupDescription("ItemGroup"));
}
Loaded += (s, e) =>
{
foreach (Expander gi in FindVisualChildren<Expander>(lv))
{
gi.IsExpanded = true;
}
};
}
奇怪的是,@ mm8发布的答案不起作用,我无法弄清楚原因。但无论如何我设法做了一个解决方法。关键是将扩展器的IsExpanded
属性绑定到ListView
的未使用的属性,例如,它的标签:
<ControlTemplate>
<Expander Header="{Binding Name}">
<Expander.IsExpanded>
<Binding RelativeSource="{RelativeSource FindAncestor, AncestorType=ListView}" Path="Tag" />
</Expander.IsExpanded>
<ItemsPresenter />
</Expander>
</ControlTemplate>
然后你可以通过在代码中设置IsExpanded
或lv.Tag = true;
来控制所有这些属性的lv.Tag = false;
属性。