c# – 使用视图模型在TreeView中展开路径

我有日期限制在TreeView.有一个日期包装类.日期分组按年和月. Wrapper类还具有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

最佳答案 TreeView绑定到CollectionViewSource.View.Groups,并且那些PropertyGroupDescription对象不包含IsSelected或IsExpanded属性,因此TreeViewItem.IsSelected和TreeViewItem.IsExpanded值具有无效绑定

您正在使用的代码将DatesViewModel.IsExpanded设置为true.您可以通过更改日期模板来验证这一点,以显示IsExpanded的值

我建议为每一层(年,月和日)创建类,并让它们都继承自TreeNodeBase类,类包含IsSelected,IsExpanded和ObservableCollection< TreeNodeBase>的属性.儿童.不要忘记为您的子项连接一个PropertyChange通知,以便在更改TreeNodeBase.IsExpanded时,父对象的IsExpanded值也会更改

点赞