【问题标题】:Sort and Group ListItems in a WPF ListBox- GroupItem collapse and expand在 WPF ListBox 中对 ListItem 进行排序和分组 - GroupItem 折叠和展开
【发布时间】:2011-06-26 13:18:28
【问题描述】:

我对列表框项目进行排序和分组,为此我使用 CollectioView。

从视图模型类中,我在 ListBox ItemSource 属性上绑定集合,就是这样。

    public BindableCollection<UserInfo> Friends
    {
        get { return _friends; }
        set
        {
            _friends = value;
            NotifyOfPropertyChange(() => Friends);
        }
    }

ListBox items 是 UserInfo 的类型。

当我初始化 ListBox 时,我使用此方法对项目进行排序和分组。

    private ICollectionView _currentView;

    //...

    private void SortContactList()
    {
        _currentView = CollectionViewSource.GetDefaultView(Friends);

        _currentView.GroupDescriptions.Add(new PropertyGroupDescription("TextStatus"));

        _currentView.SortDescriptions.Add(new SortDescription("TextStatus", ListSortDirection.Ascending));

        _currentView.SortDescriptions.Add(new SortDescription("Nick", ListSortDirection.Ascending));
    }

TextStatus 和 Nick 是 userInfo 类的属性。

我在 Listbox GroupStyle 中使用。这是它:

    <Style x:Key="MessengerView_ToogleBtn" TargetType="{x:Type ToggleButton}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ToggleButton}">
                    <Image x:Name="img" Source="/images/icons/Collapse.png" />
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked" Value="False">
                            <Setter TargetName="img" Property="Source" Value="/images/icons/Expand.png" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>


   <GroupStyle>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type GroupItem}">
                                    <ControlTemplate.Triggers>
                                        <DataTrigger Binding="{Binding Path=IsBottomLevel}" Value="True">
                                            <Setter TargetName="gridTemplate" Property="Grid.Background" Value="White" />
                                        </DataTrigger>
                                    </ControlTemplate.Triggers>
                                    <Grid>
                                        <Grid.RowDefinitions>
                                            <RowDefinition />
                                            <RowDefinition />
                                        </Grid.RowDefinitions>
                                        <Grid Background="Black" 
                                                  x:Name="gridTemplate" 
                                                  Height="26" 
                                                  VerticalAlignment="Center">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="Auto" />
                                                <ColumnDefinition Width="100" />
                                                <ColumnDefinition Width="45" />
                                            </Grid.ColumnDefinitions>

                                            <ToggleButton x:Name="btnShowHide" 
                                                          IsChecked="True" 
                                                          Style="{StaticResource MessengerView_ToogleBtn}"/>

                                            <TextBlock Style="{StaticResource MessengerView_LbGroupHeader_TextBlock}"  
                                                       Text="{Binding Path=Name}" 
                                                       Grid.Column="1"/>
                                            <TextBlock TextAlignment="Left" Style="{StaticResource MessengerView_LbGroupHeader_TextBlock}" 
                                                       Grid.Column="2" 
                                                       Text="{Binding Path=ItemCount}"/>

                                        </Grid>

                                        <ItemsPresenter Visibility="{Binding ElementName=btnShowHide, Path=IsChecked,
                                                                            Converter={StaticResource booleanToVisibilityConverter}}"
                                                            Margin="3,3,3,3"
                                                            Grid.Row="1"  />

                                    </Grid>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>

如果我运行应用程序,它会显示在这张图片上。

1。

我编辑 ListBox 的源属性(添加、删除、更新),编辑完列表框后我在 CollectionView 上调用 Refresh 方法。

            _currentView.Refresh();

问题是 GroupItem 是折叠的,我在所有 GroupItem 上调用 Refresh 方法展开。

例如。

GroupItem 1 正在折叠。

GroupItem 2 已扩展。

GroupItem 3 正在折叠。

调用Refresh ListBox前的样子如下图:

我在 CollectionView 上调用 Refresh 方法并展开所有 GroupItems。我想保持原来的状态,怎么实现呢?

调用Refresh Lisbox后看起来像在顶部的第一张图片。

【问题讨论】:

  • 我认为问题出在这里:
  • 我错过了可见性绑定。好的,我试试看如何绑定ToggleButton。

标签: wpf sorting listbox groupstyle


【解决方案1】:

解决方法并不完美。正如我所说,最好将 ViewModels 与您自己的分组一起使用,但这需要更多的代码。

您需要两个事件处理程序:

    private Dictionary<string, bool?> expandStates = new Dictionary<string, bool?>();

    private void Grid_Loaded(object sender, RoutedEventArgs e)
    {
        var grid = (Grid)sender;
        var dc = grid.DataContext as CollectionViewGroup;
        var groupName = (string)dc.Name;

        //If the dictionary contains the current group, retrieve a saved state of the group
        if (this.expandStates.ContainsKey(groupName))
        {
            var btn = (ToggleButton)grid.FindName("btnShowHide");
            btn.IsChecked = this.expandStates[groupName];
        } //Else add default state
        else this.expandStates.Add(groupName, true);

    }

    private void btnShowHide_Click(object sender, RoutedEventArgs e)
    {
        var btn = (ToggleButton)sender;
        var dc = (CollectionViewGroup)btn.DataContext;
        var groupName = (string)dc.Name;

        //Loaded event is fired earlier than the Click event, so I'm sure that the dictionary contains the key
        this.expandStates[groupName] = btn.IsChecked; //Save the current state
    }

它们在这里与控件绑定:

<ControlTemplate TargetType="{x:Type GroupItem}">
    <Grid Loaded="Grid_Loaded">

这里:

<ToggleButton x:Name="btnShowHide" Click="btnShowHide_Click" IsChecked="True" Margin="3.5" />

如果您在外部字典中的某处为 GroupItem 定义模板,则必须使用 UserControl 才能访问代码隐藏。

【讨论】:

猜你喜欢
  • 2010-12-26
  • 2015-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-15
  • 2016-08-25
  • 1970-01-01
相关资源
最近更新 更多