【问题标题】:How to add a Command to the items in a databound TreeView如何向数据绑定 TreeView 中的项目添加命令
【发布时间】:2010-11-12 23:45:41
【问题描述】:

如何将 WPF DelegateCommands 添加到绑定到 XmlDataProviderTreeView 中的项目中?我正在使用 MVVM 模式和复合 WPF,我希望在用户双击 TreeView 中的项目时调用该命令。


我在 XAML 中定义了一个 TreeView,其 DataContext 设置为 XmlDataProvider

<TreeView
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    ItemsSource="{Binding XPath=/SomeTopElement/*}">
    <TreeView.Resources>
        <HierarchicalDataTemplate
            DataType="SomeElement"
            ItemsSource="{Binding XPath=child::*}">
            <TextBlock Text="{Binding XPath=@SomeAttribute}" />
        </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>

在代码的其他部分,我只是绑定到 ViewModel 中的 DelegateCommand

<MenuItem Command="{Binding NewCommand}" Header="_New" />

上面的TreeView如何做到这一点?

【问题讨论】:

    标签: data-binding mvvm treeview command cag


    【解决方案1】:

    您应该使用附加命令行为模式。 This question 回答了类似的问题,但在 ListView 中。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-17
    • 1970-01-01
    • 1970-01-01
    • 2016-05-06
    • 2016-05-12
    • 1970-01-01
    相关资源
    最近更新 更多