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

时间:2011-09-27 09:47:54

标签: c# wpf mvvm treeview grouping

我的日期与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

1 个答案:

答案 0 :(得分:2)

您的TreeView绑定到CollectionViewSource.View.Groups,而这些PropertyGroupDescription个对象不包含IsSelectedIsExpanded属性,因此您的TreeViewItem.IsSelectedTreeViewItem.IsExpanded值的绑定无效

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

的值

我建议为每个层(年,月和日)创建类,并让它们全部继承自TreeNodeBase类,其中包含IsSelectedIsExpanded的属性和ObservableCollection<TreeNodeBase> Children。不要忘记为PropertyChange添加Children通知,这样当TreeNodeBase.IsExpanded发生更改时,父对象的IsExpanded值也会发生变化