2009-07-22 92 views
1

如何将WPF DelegateCommand s添加到绑定到XmlDataProviderTreeView中的项目?我正在使用MVVM模式和Composite WPF,并且我想要在用户双击TreeView中的某个项目时调用该命令。如何将命令添加到数据绑定中的项目TreeView


我在XAML其DataContext被设置为XmlDataProvider限定的TreeView

<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 [email protected]}" /> 
     </HierarchicalDataTemplate> 
    </TreeView.Resources> 
</TreeView> 

在代码我简单地结合到DelegateCommand在视图模型的其他部分:

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

这怎么能用上面的TreeView完成?

回答

2

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

+0

我还发现StackOverflow另一个问题(http://stackoverflow.com/questions/926451/how-can-i-attach-two-attached-behaviors-to-one-xaml-element)和一个例子使用TreeView附加行为(http://www.codeproject.com/KB/WPF/AttachedBehaviors.aspx) – 2009-07-22 13:00:31

相关问题