現在、私は日付を年と月(内部グループ)でグループ化するアプリケーションを開発しています。WPF CollectionViewSourceのグループプロパティ
私はTreeView
にグループ化についてのいくつかの例をGoogleで検索して、そのような解決策を見つけた:私はそうで私のTreeView
とするためのテンプレートを作成し、CollectionViewSource
ためSource
として日付のプレーンなリストを使用するグループと種類を定義します。
<TreeView ItemsSource={Binding Source={StaticResource CVS}, Path=Groups} />
:
操作例(しかし唯一のグループの入れ子の場合)は、そのようなコードを含みます。
私はMVVMパターンを使用しているので、ビューモデルのプロパティ(リソースではない)としてCollectionViewSource
を定義します。
<TreeView ItemsSource={Binding Path=CVS.View} />
が、それは動作しません:
<TreeView ItemsSource={Binding Path=CVS.Groups} />
など:
は私が間違って足に立ったが、私はポート上記のコードをできなかったことが、私はそうしようとしました。 CollectionViewSource
には、プロパティGroup
がありません。
私は間違っていますか?
UPDATE:
完全なソースコード:DayWorkInfoViewModel.csで
:DayWorkViewModel.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
}
:
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
}
WorkView.xamlで
: WorkView.xaml.csで
<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
}
私は解決策を見つけた: 'Group'はない' 'CollectionViewSource' View'の財産です。正しい方法: ' ' –