WPF ListView实时分组 [英] WPF ListView Live Grouping
问题描述
我有一个动态分组的列表视图,我用来添加组描述的代码是。
I have a listview with dynamic grouping, the code I use to add group descriptions is .
PropertyGroupDescription groupDescription = new PropertyGroupDescription("CheckSum");
view = (CollectionView)CollectionViewSource.GetDefaultView(SourceList);
if (view != null)
view.GroupDescriptions.Add(groupDescription);
我想要分组生活,但无法弄清楚如何做到这一点。当我尝试使用相同代码的ICollectionViewLiveShaping时,它什么都不做。
I would like the grouping to be live but have not been able to figure out how to do it. When I try to use ICollectionViewLiveShaping with the same code, it does nothing.
这是我正在使用的值转换器,它是转换的MD5值组成组名称(例如组1,组2)等。它工作正常但经过大量滚动顶部后,列表的顶部和底部扰乱了分组的顺序。
Here is the value converter i am using, it is to convert MD5 values into group names(e.g Group 1, Group 2) etc. It is working fine but after a lot of scrolling top the top and bottom of the list disturbs the order of grouping.
class GroupNameConverter : IValueConverter
{
public int i = 0;
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (i <= MainWindow.CheckSums.Count-1)
{
i++;
return "Group " + i.ToString();
}
else
{
i--;
return "Group " + i.ToString();
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
首先分组很好,但在向上和向下滚动后,群组名称会全部乱序。
The grouping is fine at first, but after scrolling up and down the group names get all out of order.
我知道问题可能在值转换器中,但无法弄清楚如何修复它。任何帮助将不胜感激。
I know the problem could be in the value converter but cant figure out how to fix it. Any help will be greatly appreciated.
推荐答案
嗨Waleed,
Hi Waleed,
>>我希望分组能够直播,但还是无法计算如何做到这一点。当我尝试使用相同的
代码的ICollectionViewLiveShaping时,它什么都不做。
根据您的描述和代码,您想使用
ICollectionViewLiveShaping to group in
ListView,但你有一些问题,所以请看看下面的代码:
<Grid DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Window}, Path=userview}">
<ListView Name="lvUsers" ItemsSource="{Binding}">
<ListView.View>
<GridView>
<GridViewColumn
Width="120"
DisplayMemberBinding="{Binding Name}"
Header="Name" />
<GridViewColumn
Width="50"
DisplayMemberBinding="{Binding Age}"
Header="Age" />
</GridView>
</ListView.View>
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Expander IsExpanded="True">
<Expander.Header>
<StackPanel Orientation="Horizontal">
<TextBlock
VerticalAlignment="Bottom"
FontSize="22"
FontWeight="Bold"
Foreground="Gray"
Text="{Binding Name}" />
<TextBlock
Margin="10,0,0,0"
VerticalAlignment="Bottom"
FontSize="22"
FontStyle="Italic"
FontWeight="Bold"
Foreground="Green"
Text="{Binding ItemCount}" />
<TextBlock
VerticalAlignment="Bottom"
FontSize="22"
FontStyle="Italic"
Foreground="Silver"
Text=" item(s)" />
</StackPanel>
</Expander.Header>
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ListView.GroupStyle>
</ListView>
</Grid>
public partial class Window2 : Window
{
public ListCollectionView userview { get; set; }
public Window2()
{
InitializeComponent();
ObservableCollection<User> items = new ObservableCollection<User>();
items.Add(new User() { Name = "John Doe", Age = 42, Sex = SexType.Male });
items.Add(new User() { Name = "Jane Doe", Age = 39, Sex = SexType.Female });
items.Add(new User() { Name = "Sammy Doe", Age = 13, Sex = SexType.Male });
userview= new ListCollectionView(items);
userview.IsLiveGrouping = true;
userview.GroupDescriptions?.Add(new PropertyGroupDescription("Sex"));
//CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(lvUsers.ItemsSource);
//PropertyGroupDescription groupDescription = new PropertyGroupDescription("Sex");
//view.GroupDescriptions.Add(groupDescription);
}
}
public enum SexType { Male, Female };
public class User:ViewModelBase
{
private string _Name;
public string Name
{
get { return _Name; }
set
{
_Name = value;
RaisePropertyChanged("Name");
}
}
private int _Age;
public int Age
{
get { return _Age; }
set
{
_Age = value;
RaisePropertyChanged("Age");
}
}
private string _Mail;
public string Mail
{
get { return _Mail; }
set
{
_Mail = value;
RaisePropertyChanged("Mail");
}
}
private SexType _Sex;
public SexType Sex
{
get { return _Sex; }
set
{
_Sex = value;
RaisePropertyChanged("Sex"); }
}
}
最好的问候,
Cherry
这篇关于WPF ListView实时分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!