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

发布于 2024-12-06 20:18:35 字数 4163 浏览 0 评论 0原文

我有绑定到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

姜生凉生 2024-12-13 20:18:35

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

绑定DatesViewModel.IsExpanded IS 在您使用的代码中设置为 true。您可以通过更改 Day 模板以显示 IsExpanded 的值来验证这一点。

我建议为每个层(年、月和日)创建类,并让它们全部继承自 >TreeNodeBase 类,其中包含 IsSelectedIsExpandedObservableCollection的属性孩子们。不要忘记为您的 Children 连接一个 PropertyChange 通知,以便当 TreeNodeBase.IsExpanded 发生更改时,父对象的 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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文