gpt4 book ai didi

c# - 分组 ListView WPF

转载 作者:太空宇宙 更新时间:2023-11-03 22:59:17 25 4
gpt4 key购买 nike

这很令人沮丧。我做了一个测试应用程序,它看起来像这样:

查看:

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

View 模型:

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


所以,现在我想将这个“功能”(分组 ListView )移植到我的“真实”应用程序中,但我没有让它与分组一起工作。有关详细信息,我的观点:

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

现在我的问题:在调试器中,我可以看到两个查看器,它们都有不同的“类型”:enter image description here

但两者都显示为“Mod”。不是观众:enter image description here

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

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

最佳答案

我终于找到了解决问题的方法。问题:在我的示例中,我在 ListView 将“绘制”之前添加项目。

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

我们开始吧,我将“LiveGroupingProperties”添加到 CollectionViewSource。

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

关于c# - 分组 ListView WPF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43918345/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com