如何访问WPF GroupItem的模板中的元素

问题描述 投票:-1回答:2

我有一个充满了一些物品的ListView。这些项目有两个属性,ItemNameItemGroup,我想用它们的第二个属性对它们进行分组。所以我写了这样的东西:

<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"));
}

现在一切顺利。但问题是,有时我想扩展代码背后的所有Expanders,我发现无法访问它们并将其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;
    }
}
c# wpf controltemplate expander
2个回答
1
投票

您可以使用递归方法和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;
            }
        }
    }
}

编辑:如果您想在设置ItemsSourceListView属性后立即执行此操作,则需要等到容器已创建。处理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;
        }
    };
}

0
投票

奇怪的是,@ mm8发布的答案不起作用,我无法弄清楚原因。但无论如何我设法做了一个解决方法。关键是将扩展器的IsExpanded属性绑定到ListView的未使用的属性,例如,它的标签:

<ControlTemplate>
    <Expander Header="{Binding Name}">
        <Expander.IsExpanded>
            <Binding RelativeSource="{RelativeSource FindAncestor, AncestorType=ListView}" Path="Tag" />
        </Expander.IsExpanded>
        <ItemsPresenter />
    </Expander>
</ControlTemplate>

然后你可以通过在代码中设置IsExpandedlv.Tag = true;来控制所有这些属性的lv.Tag = false;属性。

© www.soinside.com 2019 - 2024. All rights reserved.