开发者

WPF CollectionViewSource's Group property

开发者 https://www.devze.com 2023-03-23 22:33 出处:网络
Currently I\'m developing an applic开发者_StackOverflowation where dates are grouped by Years and Months (inner groups).

Currently I'm developing an applic开发者_StackOverflowation 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>
0

精彩评论

暂无评论...
验证码 换一张
取 消