双击时使TreeViewItem控件执行命令

时间:2011-03-24 15:30:32

标签: wpf xaml mvvm command treeviewitem

我正在寻找一种方法,在WPF中将命令绑定到TreeViewItem,所以当我双击TreeViewItem时,ICommand在我的ViewModel(MVVM模式)中执行。 与Visual Studio Solution Explorer非常相似。

我希望我可以使用类似AttachedCommandBehaviour之类的东西,但这不起作用。我想这是因为TreeViewItem本身不支持命令。

这是我的测试实现,它不起作用:

<TreeViewItem Header="Opret produktions ordre">
    <acb:CommandBehaviorCollection.Behaviors>
        <acb:BehaviorBinding Event="MouseLeftButtonDown" Command="{Binding TestCommand}"/>
    </acb:CommandBehaviorCollection.Behaviors>
</TreeViewItem>

有没有人建议如何使这项工作?或许你知道一个控件给我这个功能? 我当然希望有一个MVVM友好的解决方案。最好全部在XAML中。

1 个答案:

答案 0 :(得分:0)

您是否尝试过CodePlex Expression Blend sample library?{/ p>的InvokeDataCommand触发器

<i:EventTrigger EventName="Click">
      <si:InvokeDataCommand Command="{Binding ShoppingCart.CheckOutCommand}"/>
</i:EventTrigger>

如果您使用的是WPF4,则需要修复版本,请在此处阅读相关问题:http://blog.thekieners.com/2010/11/09/expression-blend-samples-not-working-with-silverlight-4-and-wpf-4/

从此处下载示例解决方案(这是更新版本):http://expressionblend.codeplex.com/workitem/8148