问题描述
我有日期一定到TreeView控件。有日期的包装类。按年月日期群体。包装类也有IsSelected和IsExpanded属性:
I have dates bounded to TreeView. There is a wrapper class for date. Dates groups by years and months. Wrapper class also has IsSelected and IsExpanded properties:
public sealed class DateViewModel : NotificationObject, IEditableObject
{
#region properties
bool _isSelected;
public bool IsSelected
{
get
{
return _isSelected;
}
set
{
if (_isSelected != value)
{
_isSelected = value;
RaisePropertyChanged(() => IsSelected);
}
}
}
bool _isExpanded;
public bool IsExpanded
{
get
{
return _isExpanded;
}
set
{
if (_isExpanded != value)
{
_isExpanded = value;
RaisePropertyChanged(() => IsExpanded);
}
}
}
DateTime _date;
public DateTime Date
{
get
{
return _date;
}
set
{
if (_date != value)
{
_date = value;
RaisePropertyChanged(() => Date);
RaisePropertyChanged(() => Year);
RaisePropertyChanged(() => Month);
RaisePropertyChanged(() => MonthName);
RaisePropertyChanged(() => 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
}
$ B DateViewModel的$ b
的ObservableCollection被用作对的ItemsSource树视图。由CollectionViewSource ANS的DataTemplates日期群体:
ObservableCollection of DateViewModel is used as ItemsSource for TreeView. Dates groups by CollectionViewSource ans DataTemplates:
<DataTemplate x:Key="DayTemplate">
<TextBlock x:Name="textBlock"
FontSize="14"
Text="{Binding Path=Day}" />
</DataTemplate>
<HierarchicalDataTemplate x:Key="MonthTemplate"
ItemsSource="{Binding Path=Items}"
ItemTemplate="{StaticResource DayTemplate}">
<TextBlock Text="{Binding Path=Name}" />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate x:Key="YearTemplate"
ItemsSource="{Binding Path=Items}"
ItemTemplate="{StaticResource MonthTemplate}">
<TextBlock>
<Run Text="{Binding Path=Name, Mode=OneWay}" />
<Run Text="y" />
</TextBlock>
</HierarchicalDataTemplate>
<telerik:RadTreeView Grid.Row="1"
ItemsSource="{Binding Path=Dates.View.Groups}"
ItemTemplate="{StaticResource YearTemplate}">
<telerik:RadTreeView.ItemContainerStyle>
<Style TargetType="{x:Type telerik:RadTreeViewItem}">
<Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}" />
<Setter Property="IsExpanded" Value="{Binding Path=IsExpanded, Mode=TwoWay}" />
</Style>
</telerik:RadTreeView.ItemContainerStyle>
</telerik:RadTreeView>
的问题是:我需要使用视图模型通过IsExpanded属性设置为true扩大完整路径日期。但它没有效果。
The issue is: I need to expand full path to date using view model by setting IsExpanded property to true. But it doesn't have effect.
更新:
是我创建在码组说明。扩大代码很简单:
Yes I created group descriptions in code. The code for expanding is simple:
public sealed class DatesViewModel
{
ObservableCollection<DateViewModel> _dates = new ObservableCollection<DateViewModel>();
public CollectionViewSource Dates {get; set;}
public DatesViewModel()
{
Dates = new CollectionViewSource { Source = _dates } ;
// add groups, sorts and fill collection
...
}
// Just a sample
public void ExpandFirstDate()
{
_dates[0].IsExpanded = true;
}
}
有高于遗漏码。
此外,我准备试验样品
Also I prepared test sample TreeViewGroupingSample.7z
推荐答案
您 TreeView控件
绑定到 CollectionViewSource.View.Groups
,而那些 PropertyGroupDescription
对象不包含 IsSelected
或 IsExpanded
属性,所以你的 TreeViewItem.IsSelected
和 TreeViewItem.IsExpanded
值有一个无效的结合
Your TreeView
is binding to CollectionViewSource.View.Groups
, and those PropertyGroupDescription
objects do not contain IsSelected
or IsExpanded
properties, so your TreeViewItem.IsSelected
and TreeViewItem.IsExpanded
values have an invalid binding
您 DatesViewModel.IsExpanded
越来越设置为true的代码您正在使用。您可以通过改变你的一天模板显示 IsExpanded的值来验证这种
Your DatesViewModel.IsExpanded
IS getting set to true with the code you are using. You can verify this by changing your Day template to show the value of IsExpanded
我建议每个创建类层(年,月,日)的,并让他们都来自像它包含了 IsSelected $ C $属性
TreeNodeBase
类继承C>, IsExpanded
和的ObservableCollection< TreeNodeBase>儿童
。不要忘了勾了一个的PropertyChange
通知你的孩子
,以便当 TreeNodeBase。 IsExpanded
得到改变,父对象的 IsExpanded
价值变动太
I would recommend creating classes for each tier (Year, Month, and Day), and having them all inherit from something like a TreeNodeBase
class which contains properties for IsSelected
, IsExpanded
, and ObservableCollection<TreeNodeBase> Children
. Don't forget to hook up a PropertyChange
notification for your Children
so that when TreeNodeBase.IsExpanded
gets changed, the parent object's IsExpanded
value changes too
这篇关于使用视图模型展开路径的TreeView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!