WPF CollectionViewSource的集团物业 [英] WPF CollectionViewSource's Group property

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

问题描述

目前我正在开发,其中日期由年和月(内组)分组的应用程序。



我GOOGLE关于 TreeView控件,发现这样的解决方案:我使用日期的普通列表为来源 CollectionViewSource ,定义组和排序,我的 TreeView控件等写的模板。



工作示例(但仅限于一组嵌套)包括这样的代码:



<树视图的ItemsSource = {绑定源= {StaticResource的CVS},路径=组} />



由于我使用MVVM模式,我定义 CollectionViewSource 作为视图模型的财产(不。作为资源)



可能是我站在出师不利,但我不能口上面的代码,我试着这样做:



<树视图的ItemsSource = {绑定路径= CVS.Groups} />



<树视图的ItemsSource = {绑定路径= CVS.View} /> ,但它不工作。 CollectionViewSource 没有财产



什么?我做错了。



更新:



完整的源代码:



在DayWorkInfoViewModel.cs:

 内部密封类DayWorkInfoViewModel:ViewModelBase 
{
#地区的物业

私人的DateTime _date;

公众的DateTime日期
{
得到
{
返回_date;
}

{
如果(_DATE =价值!)
{
_date =价值;
OnPropertyChanged(日期);
OnPropertyChanged(年);
OnPropertyChanged(月);
OnPropertyChanged(MONTHNAME);
OnPropertyChanged(日);
}
}
}

公众诠释每年
{
得到
{
返回Date.Year;
}
}

公众诠释每月
{
得到
{
返回Date.Month;
}
}

公共字符串MONTHNAME
{
得到
{
返回CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(日期。月);
}
}

公众诠释天
{
得到
{
返回Date.Day;
}
}

#endregion性能
}

在DayWorkViewModel.cs:

 内部密封类WorkViewModel:PageBaseViewModel 
{
#地区的字段

私人只读IWorkService _workService;

#endregion领域

#地区的房产

私人只读的ObservableCollection< DayWorkInfoViewModel> _dayWorkInfos =新的ObservableCollection< DayWorkInfoViewModel>();

公共CollectionViewSource DayWorkInfos {搞定;组; }

私人DayWorkInfoViewModel _selectedDayWorkInfo;

公共DayWorkInfoViewModel SelectedDayWorkInfo
{
得到
{
返回_selectedDayWorkInfo;
}

{
如果(_selectedDayWorkInfo =价值!)
{
_selectedDayWorkInfo =价值;
OnPropertyChanged(SelectedDayWorkInfo);
}
}
}

#endregion性能

#地区的命令属性

#endregion命令属性

#地区构建函数

公共WorkViewModel()
{
如果(IsInDesign)
{
_workService =新SimpleWorkService( );
}
,否则
{
_workService =新WorkService();
}

DayWorkInfos =新CollectionViewSource {源= _dayWorkInfos};
DayWorkInfos.GroupDescriptions.Add(新PropertyGroupDescription(年));
DayWorkInfos.GroupDescriptions.Add(新PropertyGroupDescription(MONTHNAME));
DayWorkInfos.SortDescriptions.Add(新SortDescription(年,ListSortDirection.Ascending));
DayWorkInfos.SortDescriptions.Add(新SortDescription(月,ListSortDirection.Ascending));

DoAsync< IEnumerable的< DateTime的>>(
()=>
{
返回_workService.GetDayWorkInfos();
},
(结果)=>
{
_dayWorkInfos.Clear();

的foreach(在结果VAR DT)
{
_dayWorkInfos.Add (新DayWorkInfoViewModel {日期= DT});
}

//DayWorkInfos.View.Refresh();
},
(EXC)=>
{
ShowError(无法加载工作日期...);
},
装载工作日期...);
}

#endregion构建函数
}



在WorkView.xaml:

 <控制:PageBase.Resources> 
<的DataTemplate X:键=DayTemplate>
< TextBlock的文本={绑定路径=天}/>
< / DataTemplate中>
< HierarchicalDataTemplate X:键=MonthTemplate
的ItemsSource ={绑定路径=项目}
的ItemTemplate ={StaticResource的DayTemplate}>
< TextBlock的文本={绑定路径=日期}/>
< / HierarchicalDataTemplate>
< HierarchicalDataTemplate X:键=YearTemplate
的ItemsSource ={绑定路径=项目}
的ItemTemplate ={StaticResource的MonthTemplate}>
< TextBlock的文本={绑定路径=年}/>
< / HierarchicalDataTemplate>
< /控制:PageBase.Resources>
<网格和GT;
< Telerik的:RadTreeView保证金=10
BorderBrush =红
了borderThickness =3
的ItemsSource ={结合DayWorkInfo.Groups}
的ItemTemplate ={StaticResource的YearTemplate}/>
< /网格和GT;

在WorkView.xaml.cs:



 公共部分类WorkView:PageBase 
{
#地区构建函数
公共WorkView()
{
的InitializeComponent();
的DataContext =新WorkViewModel();
}
#endregion构建函数
}


解决方案< 。/ DIV>

我猜你正在尝试做这样的事情。

 公共部分类主窗口:窗口
{
私人CollectionViewSource CVS =新CollectionViewSource();

公共CollectionViewSource CVS
{
得到
{
返回this.cvs;
}
}
公共主窗口()
{
的InitializeComponent();
&的ObservableCollection LT; DateTime的>名单=新的ObservableCollection<&日期时间GT;();
list.Add(新日期时间(2010,2,11));
list.Add(新的DateTime(2010,7,11));
list.Add(新的DateTime(2010,7,14));
list.Add(新的DateTime(2010,2,5));
list.Add(新日期时间(2010,3,6));
list.Add(新的DateTime(2011,1,8));
list.Add(新日期时间(2011年,7,3));
list.Add(新的DateTime(2011,1,12));
list.Add(新日期时间(2011年,2,3));

this.cvs.Source =清单;
this.cvs.GroupDescriptions.Add(新PropertyGroupDescription(年));
this.cvs.GroupDescriptions.Add(新PropertyGroupDescription(月));
this.DataContext =这一点;
}
}

和XAML中:

 <窗口x:类=CollectionViewSource.MainWindow
的xmlns =http://schemas.microsoft.com/winfx/2006/ XAML /演示
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml的xmlns:系统=CLR的命名空间:系统;装配= mscorlib程序标题=主窗口身高=350宽度=525>
<网格和GT;
<树视图的ItemsSource ={绑定路径= CVS.View.Groups}>
< TreeView.Resources>
< HierarchicalDataTemplate数据类型={X:类型CollectionViewGroup}的ItemsSource ={绑定表项}>
< TextBlock的文本={绑定名称}/>
< / HierarchicalDataTemplate>
<数据类型的DataTemplate ={X:类型系统:日期时间}>
< TextBlock的文本={结合日期}/>
< / DataTemplate中>
< /TreeView.Resources>
< / TreeView的>
< /网格和GT;
< /窗GT;


Currently I'm developing an application where dates are grouped by Years and Months (inner groups).

I googled some examples about grouping in TreeView and found such solution: I use a plain list of dates as Source for CollectionViewSource, define groups and sorts, write template for my TreeView and so on.

Working examples (but only for one group nesting) includes such code:

<TreeView ItemsSource={Binding Source={StaticResource CVS}, Path=Groups} />.

Since I use MVVM pattern I define CollectionViewSource as view model's property (not as resource).

May be I stood on the wrong foot but I couldn't port the code above, I tried do so:

<TreeView ItemsSource={Binding Path=CVS.Groups} />, and so:

<TreeView ItemsSource={Binding Path=CVS.View} /> but it doesn't work. CollectionViewSource doesn't have property Group.

What I'm doing wrong?

UPDATE:

Full source code:

In DayWorkInfoViewModel.cs:

internal sealed class DayWorkInfoViewModel : ViewModelBase
{
    #region properties

    private DateTime _date;

    public DateTime Date
    {
        get
        {
            return _date;
        }
        set
        {
            if (_date != value)
            {
                _date = value;
                OnPropertyChanged("Date");
                OnPropertyChanged("Year");
                OnPropertyChanged("Month");
                OnPropertyChanged("MonthName");
                OnPropertyChanged("Day");
            }
        }
    }

    public int Year
    {
        get
        {
            return Date.Year;
        }
    }

    public int Month
    {
        get
        {
            return Date.Month;
        }
    }

    public string MonthName
    {
        get
        {
            return CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(Date.Month);
        }
    }

    public int Day
    {
        get
        {
            return Date.Day;
        }
    }

    #endregion properties
}

In DayWorkViewModel.cs:

internal sealed class WorkViewModel : PageBaseViewModel
{
    #region fields

    private readonly IWorkService _workService;

    #endregion fields

    #region properties

    private readonly ObservableCollection<DayWorkInfoViewModel> _dayWorkInfos = new ObservableCollection<DayWorkInfoViewModel>();

    public CollectionViewSource DayWorkInfos { get; set; }

    private DayWorkInfoViewModel _selectedDayWorkInfo;

    public DayWorkInfoViewModel SelectedDayWorkInfo
    {
        get
        {
            return _selectedDayWorkInfo;
        }
        set
        {
            if (_selectedDayWorkInfo != value)
            {
                _selectedDayWorkInfo = value;
                OnPropertyChanged("SelectedDayWorkInfo");
            }
        }
    }

    #endregion properties

    #region command properties

    #endregion command properties

    #region ctors

    public WorkViewModel()
    {
        if (IsInDesign)
        {
            _workService = new SimpleWorkService();
        }
        else
        {
            _workService = new WorkService();
        }

        DayWorkInfos = new CollectionViewSource { Source = _dayWorkInfos };
        DayWorkInfos.GroupDescriptions.Add(new PropertyGroupDescription("Year"));
        DayWorkInfos.GroupDescriptions.Add(new PropertyGroupDescription("MonthName"));
        DayWorkInfos.SortDescriptions.Add(new SortDescription("Year", ListSortDirection.Ascending));
        DayWorkInfos.SortDescriptions.Add(new SortDescription("Month", ListSortDirection.Ascending));

        DoAsync<IEnumerable<DateTime>>(
            () =>
            {
                return _workService.GetDayWorkInfos();
            },
            (result) =>
            {
                _dayWorkInfos.Clear();

                foreach (var dt in result)
                {
                    _dayWorkInfos.Add(new DayWorkInfoViewModel { Date = dt });
                }

                //DayWorkInfos.View.Refresh();
            },
            (exc) =>
            {
                ShowError("Couldn't load work dates...");
            },
            "Loading work dates...");
    }

    #endregion ctors
}

In WorkView.xaml:

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

In WorkView.xaml.cs:

public partial class WorkView : PageBase
{
    #region ctors
    public WorkView()
    {
        InitializeComponent();
        DataContext = new WorkViewModel();
    }
    #endregion ctors
}

解决方案

I guess you are trying to do something like this.

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

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

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

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