【问题标题】:WPF ListBox GroupItem expander should be expanded if an item is added to respective group如果将项目添加到相应的组,则应扩展 WPF ListBox GroupItem 扩展器
【发布时间】:2016-12-19 13:21:08
【问题描述】:

我有一个包含组项列表的列表框,其中包含“扩展器”中的每个组项,并且它总是在第一次实例中折叠。当一个项目被添加到特定组的列表框中时,那时该组的相应扩展器应该被展开(IsExpanded=true)。

以下是我目前尝试过的风格。我在这里有什么遗漏吗?

<Style x:Key="GroupContainerStyleA" TargetType="{x:Type GroupItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Expander IsExpanded="False" Style="{StaticResource ExpanderStyle}">
                            <Expander.Header>
                                <Border>
                                    <TextBlock Text="{Binding Path=Name}" FontWeight="Bold" Foreground="White"/>
                                </Border>
                            </Expander.Header>
                            <Border Background="White" Margin="1.5,0,1.5,1.5" BorderBrush="{StaticResource SideButtonBackgroundBrushKey}" BorderThickness="0.5">
                                <ItemsPresenter Margin="5,0,0,5"/>
                            </Border>
                        </Expander>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

【问题讨论】:

    标签: wpf listbox


    【解决方案1】:

    您需要将 Expander 的 IsExpanded 属性设置为 true 才能展开它。例如,您可以通过订阅组中项目的 CollectionChanged 事件来做到这一点:

    private void Expander_Loaded(object sender, RoutedEventArgs e)
    {
        Expander expander = sender as Expander;
        CollectionViewGroup cvs = expander.DataContext as CollectionViewGroup;
        if (cvs != null)
        {
            INotifyCollectionChanged coll = cvs.Items as INotifyCollectionChanged;
            if (coll != null)
            {
                WeakEventManager<INotifyCollectionChanged, NotifyCollectionChangedEventArgs>.AddHandler(coll, "CollectionChanged",
                    (ss, ee) => expander.IsExpanded = true);
            }
        }
    
    }
    

    <Style TargetType="{x:Type GroupItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <Expander IsExpanded="False" Loaded="Expander_Loaded">
                        <Expander.Header>
                            <Border>
                                <TextBlock Text="{Binding Path=Name}" FontWeight="Bold" Foreground="White"/>
                            </Border>
                        </Expander.Header>
                        <Border Background="White" Margin="1.5,0,1.5,1.5" BorderBrush="Black" BorderThickness="0.5">
                            <ItemsPresenter Margin="5,0,0,5"/>
                        </Border>
                    </Expander>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    或者您可以将 Expander 的 IsExpanded 属性绑定到您的某个源属性。请参阅以下问题了解更多信息。

    wpf datagrid automatically expand first group

    【讨论】:

    • 我找不到 WeakEventManager 的命名空间
    • 它位于 .NET Framework 4.5+ 的 WindowsBase 程序集中的 System.Windows 命名空间中。使用 System.Windows 添加;在代码文件的顶部。有关 .NET 中弱事件模式的更多信息,请参阅以下文章:codeproject.com/articles/738109/…。另一种选择是使用直接引用连接事件处理程序:coll.CollectionChanged += (ss, ee) => expander.IsExpanded = true;
    • 哦,我的目标版本是4.5,我现在能做什么??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多