使用 prism 将命令附加到 wpf 中的 TreeView

发布于 2024-08-13 10:46:25 字数 159 浏览 2 评论 0原文

如何在 TreeView 中使用 DelegateCommand 来获取 Expanded 事件?

我应该使用 DelegateCommand 还是还有其他方法?

谢谢

How do I use a DelegateCommand in a TreeView to get the Expanded event?

Should I be using the DelegateCommand or is there another way?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

你不是我要的菜∠ 2024-08-20 10:46:25

既然您提到了 Prism,我假设您有一个控制器或 ViewModel 附加到包含 TreeView 的视图...

在这种情况下,公开一个布尔属性 IsExpanded

    private bool _isExpanded;
    public bool IsExpanded
    {
        get { return _isExpanded; }
        set
        {
            if (value != _isExpanded)
            {
                _isExpanded = value;
                RaisePropertyChanged("IsExpanded");
                //  Apply custom logic here...
            }
        }
    }

Now 将此属性挂接到 TreeView,您需要应用以下内容TreeView 资源中的样式(或视情况在可视化树中进一步向上)

<Style TargetType="{x:Type TreeViewItem}">
    <Setter Property="IsExpanded" Value="{Binding Path=IsExpanded, Mode=TwoWay}" />
</Style>

注意:您还可以使用类似的技术来连接 IsSelected 属性 - 也非常有用!

Since you are mentioning Prism, I assume you have a controller or ViewModel attached to the view containing your TreeView...

That being the case, expose a boolean property IsExpanded

    private bool _isExpanded;
    public bool IsExpanded
    {
        get { return _isExpanded; }
        set
        {
            if (value != _isExpanded)
            {
                _isExpanded = value;
                RaisePropertyChanged("IsExpanded");
                //  Apply custom logic here...
            }
        }
    }

Now to hook this property up to the TreeView, you need to apply the following style in the TreeView's resources (or further up the Visual tree as appropriate)

<Style TargetType="{x:Type TreeViewItem}">
    <Setter Property="IsExpanded" Value="{Binding Path=IsExpanded, Mode=TwoWay}" />
</Style>

NB: You can also use a similar technique to hook up the IsSelected property - also very useful!!

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