使用视图模型在 TreeView 中展开路径
我有绑定到 TreeView 的日期。有一个日期包装类。日期按年和月分组。包装类还具有 IsSelected 和 IsExpanded 属性:
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
}
DateViewModel 的 ObservableCollection 用作 TreeView 的 ItemsSource。按 CollectionViewSource 和 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 来使用视图模型扩展日期的完整路径。但没有效果。
更新:
是的,我在代码中创建了组描述。扩展的代码很简单:
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;
}
}
上面缺少代码。
我还准备了测试示例 TreeViewGroupingSample.7z
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
}
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>
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.
UPDATE:
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;
}
}
There is missing code above.
Also I prepared test sample TreeViewGroupingSample.7z
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
TreeView
绑定到CollectionViewSource.View.Groups
,并且这些PropertyGroupDescription
对象不包含IsSelected
或IsExpanded
属性,因此您的TreeViewItem.IsSelected
和TreeViewItem.IsExpanded
值具有无效绑定
DatesViewModel.IsExpanded
IS 在您使用的代码中设置为 true。您可以通过更改 Day 模板以显示IsExpanded
的值来验证这一点。我建议为每个层(年、月和日)创建类,并让它们全部继承自
>TreeNodeBase
类,其中包含IsSelected
、IsExpanded
和ObservableCollection的属性孩子们。不要忘记为您的
Children
连接一个PropertyChange
通知,以便当TreeNodeBase.IsExpanded
发生更改时,父对象的IsExpanded
值也发生变化Your
TreeView
is binding toCollectionViewSource.View.Groups
, and thosePropertyGroupDescription
objects do not containIsSelected
orIsExpanded
properties, so yourTreeViewItem.IsSelected
andTreeViewItem.IsExpanded
values have an invalid bindingYour
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 ofIsExpanded
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 forIsSelected
,IsExpanded
, andObservableCollection<TreeNodeBase> Children
. Don't forget to hook up aPropertyChange
notification for yourChildren
so that whenTreeNodeBase.IsExpanded
gets changed, the parent object'sIsExpanded
value changes too