分组ListView WPF [英] Grouping ListView WPF

查看:45
本文介绍了分组ListView WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

令人沮丧.我做了一个Testapplication,看起来像这样:

It's frustrating. I had made a Testapplication, which look like this:

查看:

<Window.Resources>
        <CollectionViewSource x:Key="GroupedItems" Source="{Binding Viewers}">
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="Sort" />
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>
    </Window.Resources>
    <Grid>
        <ListView
            Width="244"
            Height="184"
            Margin="46,85,0,0"
            HorizontalAlignment="Left"
            VerticalAlignment="Top"
            ItemsSource="{Binding Source={StaticResource GroupedItems}}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <WrapPanel>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock
                                Margin="10,0,0,0"
                                VerticalAlignment="Center"
                                FontWeight="Bold"
                                Text="{Binding Path=Name}" />
                        </StackPanel>
                    </WrapPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
            <ListView.GroupStyle>
                <GroupStyle>
                    <GroupStyle.ContainerStyle>
                        <Style TargetType="{x:Type GroupItem}">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate>
                                        <Expander
                                            BorderThickness="0"
                                            DataContext="{Binding Items}"
                                            IsExpanded="True">
                                            <Expander.Header>
                                                <StackPanel Orientation="Horizontal">
                                                    <TextBlock
                                                        VerticalAlignment="Top"
                                                        FontSize="22"
                                                        FontWeight="Bold"
                                                        Foreground="Gray"
                                                        Text="{Binding Sort}" />
                                                </StackPanel>
                                            </Expander.Header>
                                            <ItemsPresenter />
                                        </Expander>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </GroupStyle.ContainerStyle>
                </GroupStyle>
            </ListView.GroupStyle>
        </ListView>

ViewModel:

ViewModel:

public class MainWindowViewModel {
        public ObservableCollection<MainWindowModel.Viewer> Viewers { get; set; }

        public MainWindowViewModel() {
            Viewers = new ObservableCollection<MainWindowModel.Viewer> {
                new MainWindowModel.Viewer {
                    Name = "Hans",
                    Sort = MainWindowModel.SortDir.Admin
                },
                new MainWindowModel.Viewer {
                    Name = "Peter",
                    Sort = MainWindowModel.SortDir.Mod
                },
                new MainWindowModel.Viewer {
                    Name = "Frank",
                    Sort = MainWindowModel.SortDir.Admin
                },
                new MainWindowModel.Viewer {
                    Name = "Bilbo",
                    Sort = MainWindowModel.SortDir.Admin
                },
            };
        }
    }

型号:

public class MainWindowModel {
        public class Viewer {
            public string Name { get; set; }
            public SortDir Sort { get; set; }
        }


        public enum SortDir {
            Admin,
            Mod,
        }
    }

是的,一切正常.我得到了预期的结果.

Yeah, everything works. I get the expected result.

因此,现在我想将此功能"(分组的列表视图)移植到我的真实"应用程序中,但是我无法使其与分组一起使用.有关更多详细信息,请查看我的视图:

So, now I want to port this "feature" (grouped listview) to my 'real' application, but I dont get it to work with grouping. For more details, my View:

<controls:MetroContentControl.Resources>
        <CollectionViewSource x:Key="GroupedItems" Source="{Binding ChatHandler.Viewers}">
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="Type" />
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>
    </controls:MetroContentControl.Resources>
......
<ListView
            Name="lvUsers"
            Grid.Column="1"
            ItemsSource="{Binding Source={StaticResource GroupedItems}}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <WrapPanel>
                        <StackPanel Orientation="Horizontal">
                            <Rectangle
                                Name="Mod"
                                Width="24"
                                Height="24"
                                Fill="Black">
                                <Rectangle.OpacityMask>
                                    <VisualBrush Stretch="Fill" Visual="{StaticResource appbar_crown}" />
                                </Rectangle.OpacityMask>
                                <Rectangle.Style>
                                    <Style TargetType="Rectangle">
                                        <Setter Property="Visibility" Value="Hidden" />
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding Path=IsMod, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Value="True">
                                                <Setter Property="Visibility" Value="Visible" />
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </Rectangle.Style>
                            </Rectangle>
                            <Rectangle
                                Name="Sub"
                                Width="24"
                                Height="24"
                                Fill="Black">
                                <Rectangle.OpacityMask>
                                    <VisualBrush Stretch="Fill" Visual="{StaticResource appbar_heart}" />
                                </Rectangle.OpacityMask>
                                <Rectangle.Style>
                                    <Style TargetType="Rectangle">
                                        <Setter Property="Visibility" Value="Hidden" />
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding Path=IsSub}" Value="True">
                                                <Setter Property="Visibility" Value="Visible" />
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </Rectangle.Style>
                            </Rectangle>
                            <TextBlock
                                Margin="10,0,0,0"
                                VerticalAlignment="Center"
                                FontWeight="Bold"
                                Text="{Binding Path=Name}" />
                        </StackPanel>
                    </WrapPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
            <ListView.GroupStyle>
                <GroupStyle>
                    <GroupStyle.ContainerStyle>
                        <Style TargetType="{x:Type GroupItem}">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate>
                                        <Expander
                                            Background="Transparent"
                                            BorderThickness="0"
                                            DataContext="{Binding Items}"
                                            Foreground="Transparent"
                                            IsExpanded="True">
                                            <Expander.Header>
                                                <StackPanel Orientation="Horizontal">
                                                    <TextBlock
                                                        VerticalAlignment="Top"
                                                        FontSize="22"
                                                        FontWeight="Bold"
                                                        Foreground="Gray"
                                                        Text="{Binding Type}" />
                                                </StackPanel>
                                            </Expander.Header>
                                            <ItemsPresenter />
                                        </Expander>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </GroupStyle.ContainerStyle>
                </GroupStyle>
            </ListView.GroupStyle>
            <ListView.ContextMenu>
                <ContextMenu Name="ViewerContextMenu">
                    <MenuItem Command="{Binding MuteCommand}" Header="Mute Viewer" />
                    <MenuItem Command="{Binding UnmuteCommand}" Header="Unmute Viewer" />
                    <MenuItem Command="{Binding ModCommand}" Header="Mod Viewer" />
                    <MenuItem Command="{Binding UnmodCommand}" Header="Unmod Viewer" />
                    <MenuItem Command="{Binding ShowUserInfo}" Header="User Information" />
                </ContextMenu>
            </ListView.ContextMenu>
        </ListView>

型号:

[PropertyChanged.ImplementPropertyChanged]
        public class Viewers {
            public bool IsMod { get; set; }
            public bool IsSub { get; set; }
            public string Name { get; set; }
            public string TwitchID { get; set; }
            public SortDirectionListView Type { get; set; }
        }

        public enum SortDirectionListView {
            Admin,
            Mod,
            Subscriber,
            Follower,
            Viewer
        }

我的ViewModel仅包含与查看者一起的集合.

My ViewModel just contains the collection with the viewers.

public ObservableCollection<Models.Chat.Viewers> Viewers { get; set; }

现在我的问题是:在调试器中,我可以看到两个查看器,并且都具有不同的类型":

Now my problem: In the debugger I can see two viewers and both have diffrent "Types":

但是两者都显示为"Mod".不作为查看者:

But both shows up as "Mod". Not as a Viewer:

我没有看到我的测试应用程序和真实应用程序之间的任何区别,我也不知道第一个为什么运行,但是第二个为什么不运行.

I don't see any difference between my test application and my real application and I don't know why the first one works, but not the second one.

也许我错过了什么?感谢您的阅读!

Maybe I missed something? Thanks for reading!

推荐答案

我终于找到了解决我问题的方法.问题:在我的示例中,我在列表视图为绘制"之前添加了项目.

I have finally found a solution for my problem. The problem: In my example i add the items before the listview will be 'draw'.

在我的实际应用中,用户只会从事件添加到列表中.所以我必须告诉我的应用程序,我想刷新我的列表视图.

In my real application the users will only add to the list from an event. So i have to tell my application, that i want to refresh my listview.

然后我们去添加到CollectionViewSource"LiveGroupingProperties".

And there we go, i add to the CollectionViewSource "LiveGroupingProperties".

<CollectionViewSource
        x:Key="cvsViewers"
        IsLiveGroupingRequested="True"
        Source="{Binding ChatHandler.Viewers}">
        <CollectionViewSource.LiveGroupingProperties>
            <clr:String>Type</clr:String>
        </CollectionViewSource.LiveGroupingProperties>
        <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription PropertyName="Type" />
        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>

这篇关于分组ListView WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆