C# WPF CollectionViewSource';s群属性
目前,我正在开发一个应用程序,其中日期按年份和月份分组(内部组) 我在谷歌上搜索了一些关于在C# WPF CollectionViewSource';s群属性,c#,.net,wpf,treeview,C#,.net,Wpf,Treeview,目前,我正在开发一个应用程序,其中日期按年份和月份分组(内部组) 我在谷歌上搜索了一些关于在TreeView中分组的例子,找到了这样的解决方案:我在CollectionViewSource中使用一个简单的日期列表,如Source,定义组和排序,为我的TreeView编写模板等等 工作示例(但仅针对一个组嵌套)包括以下代码: 因为我使用MVVM模式,所以我将CollectionViewSource定义为视图模型的属性(而不是资源) 可能是我站错了脚,但我无法移植上面的代码,我尝试过这样做: ,因
TreeView
中分组的例子,找到了这样的解决方案:我在CollectionViewSource
中使用一个简单的日期列表,如Source
,定义组和排序,为我的TreeView
编写模板等等
工作示例(但仅针对一个组嵌套)包括以下代码:
因为我使用MVVM模式,所以我将CollectionViewSource
定义为视图模型的属性(而不是资源)
可能是我站错了脚,但我无法移植上面的代码,我尝试过这样做:
,因此:
但它不起作用CollectionViewSource
没有属性Group
我做错了什么
更新:
完整源代码:
在DayWorkInfo视图model.cs中:
在DayWorkViewModel.cs中:
我猜你是想做这样的事
public partial class MainWindow : Window
{
private CollectionViewSource cvs = new CollectionViewSource();
public CollectionViewSource CVS
{
get
{
return this.cvs;
}
}
public MainWindow()
{
InitializeComponent();
ObservableCollection<DateTime> list = new ObservableCollection<DateTime>();
list.Add(new DateTime(2010, 2, 11));
list.Add(new DateTime(2010, 7, 11));
list.Add(new DateTime(2010, 7, 14));
list.Add(new DateTime(2010, 2, 5));
list.Add(new DateTime(2010, 3, 6));
list.Add(new DateTime(2011, 1, 8));
list.Add(new DateTime(2011, 7, 3));
list.Add(new DateTime(2011, 1, 12));
list.Add(new DateTime(2011, 2, 3));
this.cvs.Source = list;
this.cvs.GroupDescriptions.Add(new PropertyGroupDescription("Year"));
this.cvs.GroupDescriptions.Add(new PropertyGroupDescription("Month"));
this.DataContext = this;
}
}
公共部分类主窗口:窗口
{
private CollectionViewSource cvs=new CollectionViewSource();
公共集合ViewSource CVS
{
得到
{
返回这个.cvs;
}
}
公共主窗口()
{
初始化组件();
ObservableCollection列表=新的ObservableCollection();
添加(新日期时间(2010年2月11日));
添加(新日期时间(2010年7月11日));
添加(新日期时间(2010年7月14日));
添加(新日期时间(2010年2月5日));
添加(新日期时间(2010年3月6日));
增加(新日期时间(2011年1月8日));
添加(新日期时间(2011年7月3日));
添加(新日期时间(2011年1月12日));
添加(新日期时间(2011年2月3日));
this.cvs.Source=列表;
this.cvs.GroupDescriptions.Add(新的PropertyGroupDescription(“年”);
this.cvs.GroupDescriptions.Add(新属性groupDescription(“月”);
this.DataContext=this;
}
}
以及XAML:
<Window x:Class="CollectionViewSource.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:System="clr-namespace:System;assembly=mscorlib" Title="MainWindow" Height="350" Width="525">
<Grid>
<TreeView ItemsSource="{Binding Path=CVS.View.Groups}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type CollectionViewGroup}" ItemsSource="{Binding Items}">
<TextBlock Text="{Binding Name}"/>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type System:DateTime}">
<TextBlock Text="{Binding Date}"/>
</DataTemplate>
</TreeView.Resources>
</TreeView>
</Grid>
</Window>
您的代码运行良好,但是TreeView
看起来像ListBox
-没有显示可折叠的组。非常感谢,我已经找到了解决方案。现在我在考虑如何为最低级别的组定制ItemsPanel
。我找到了解决方案:group
是视图的属性,而不是CollectionViewSource
。正确的方法:
<controls:PageBase.Resources>
<DataTemplate x:Key="DayTemplate">
<TextBlock Text="{Binding Path=Day}" />
</DataTemplate>
<HierarchicalDataTemplate x:Key="MonthTemplate"
ItemsSource="{Binding Path=Items}"
ItemTemplate="{StaticResource DayTemplate}">
<TextBlock Text="{Binding Path=Date}" />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate x:Key="YearTemplate"
ItemsSource="{Binding Path=Items}"
ItemTemplate="{StaticResource MonthTemplate}">
<TextBlock Text="{Binding Path=Year}" />
</HierarchicalDataTemplate>
</controls:PageBase.Resources>
<Grid>
<telerik:RadTreeView Margin="10"
BorderBrush="Red"
BorderThickness="3"
ItemsSource="{Binding DayWorkInfo.Groups}"
ItemTemplate="{StaticResource YearTemplate}" />
</Grid>
public partial class WorkView : PageBase
{
#region ctors
public WorkView()
{
InitializeComponent();
DataContext = new WorkViewModel();
}
#endregion ctors
}
public partial class MainWindow : Window
{
private CollectionViewSource cvs = new CollectionViewSource();
public CollectionViewSource CVS
{
get
{
return this.cvs;
}
}
public MainWindow()
{
InitializeComponent();
ObservableCollection<DateTime> list = new ObservableCollection<DateTime>();
list.Add(new DateTime(2010, 2, 11));
list.Add(new DateTime(2010, 7, 11));
list.Add(new DateTime(2010, 7, 14));
list.Add(new DateTime(2010, 2, 5));
list.Add(new DateTime(2010, 3, 6));
list.Add(new DateTime(2011, 1, 8));
list.Add(new DateTime(2011, 7, 3));
list.Add(new DateTime(2011, 1, 12));
list.Add(new DateTime(2011, 2, 3));
this.cvs.Source = list;
this.cvs.GroupDescriptions.Add(new PropertyGroupDescription("Year"));
this.cvs.GroupDescriptions.Add(new PropertyGroupDescription("Month"));
this.DataContext = this;
}
}
<Window x:Class="CollectionViewSource.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:System="clr-namespace:System;assembly=mscorlib" Title="MainWindow" Height="350" Width="525">
<Grid>
<TreeView ItemsSource="{Binding Path=CVS.View.Groups}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type CollectionViewGroup}" ItemsSource="{Binding Items}">
<TextBlock Text="{Binding Name}"/>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type System:DateTime}">
<TextBlock Text="{Binding Date}"/>
</DataTemplate>
</TreeView.Resources>
</TreeView>
</Grid>
</Window>