【发布时间】:2010-11-12 23:45:41
【问题描述】:
如何将 WPF DelegateCommands 添加到绑定到 XmlDataProvider 的 TreeView 中的项目中?我正在使用 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