Extend path in TreeView using view model

I have dates related to TreeView. There is a wrapper class for the date. Group dates by year and month. The 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 } 

The ObservableCollection of DateViewModel is used as the ItemSource for the TreeView. Date groups by CollectionViewSource and 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 problem is this: I need to extend the full path to the date using the view model by setting the IsExpanded property to true. But this has no effect.

UPDATE:

Yes I created group descriptions in code. The code for the extension 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; } } 

Missing code above.

I also prepared a test sample TreeViewGroupingSample.7z

+4
source share
1 answer

Your TreeView is a binding to CollectionViewSource.View.Groups , and those PropertyGroupDescription objects do not contain IsSelected or IsExpanded , so your TreeViewItem.IsSelected and TreeViewItem.IsExpanded values ​​have an incorrect binding

Your DatesViewModel.IsExpanded gets true with the code you are using. You can verify this by changing the Day template to show the value of IsExpanded

I would recommend creating classes for each level (Year, Month and Day), and they all inherit from something like the TreeNodeBase class, which contains properties for IsSelected , IsExpanded and ObservableCollection<TreeNodeBase> Children . Remember to include a PropertyChange notification for your Children so that when TreeNodeBase.IsExpanded changes TreeNodeBase.IsExpanded value of the parent IsExpanded object IsExpanded too

+2
source

All Articles