分组ListView WPF

时间:2017-05-11 14:29:47

标签: c# wpf listview

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

查看:

<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>

视图模型:

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,
        }
    }

是的,一切正常。 我得到了预期的结果。 Expected result

所以,现在我想移植这个&#34;功能&#34; (分组列表视图)到我的真实&#39;应用程序,但我不能让它与分组工作。 有关详细信息,请参阅我的视图:

<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只包含观众的收藏。

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

现在我的问题: 在调试器中,我可以看到两个观众,两者都有不同的类型&#34;类型&#34;:enter image description here

但两者都显示为&#34; Mod&#34;。不是作为观众:enter image description here

我没有看到我的测试应用程序和我的实际应用程序之间有任何区别,我不知道为什么第一个有效,而不是第二个。

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

1 个答案:

答案 0 :(得分:3)

我终于找到了解决问题的方法。 问题: 在我的示例中,我在列表视图之前添加项目将是&#39; draw&#39;。

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

我们去了,我添加到CollectionViewSource&#34; LiveGroupingProperties&#34;。

<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>