C# 如何将鼠标单击到HierarchycalDataTemplate中的ContentPresenter绑定到ViewModel?
我在树视图中使用了以下数据模板 如何让LeftMouseClick在ContentPresenter上以MVVM方式绑定到数据类型的属性 谢谢你的帮助(或更好的主意)C# 如何将鼠标单击到HierarchycalDataTemplate中的ContentPresenter绑定到ViewModel?,c#,wpf,xaml,mvvm,C#,Wpf,Xaml,Mvvm,我在树视图中使用了以下数据模板 如何让LeftMouseClick在ContentPresenter上以MVVM方式绑定到数据类型的属性 谢谢你的帮助(或更好的主意) ................ 附录:这很有效 <DataTemplate DataType="{x:Type r:PrinterViewModel}"> <!--Bind the ContentControl to the DataType-->
................
附录:这很有效
<DataTemplate DataType="{x:Type r:PrinterViewModel}">
<!--Bind the ContentControl to the DataType-->
<ContentControl Content="{Binding}"
ContentTemplate="{StaticResource sharedTemplate}" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<h:EventToCommand
Command="{Binding DataContext.SelectItem, RelativeSource={RelativeSource FindAncestor, AncestorType=TreeView}}"
CommandParameter="{Binding}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</ContentControl>
</DataTemplate>
补遗:事实证明,上面的Interaction.Trigger确实可以正确地将所选viewmodel发送回TreeView的DataContext。问题已解决。您希望实现什么目标?将其绑定到树节点后面的视图模型的
ICommand
属性?使ContentPresenter
作为CheckBox
?@dkozl的一部分可单击我想单击内容,并让它在viewmodel中设置一个“selected Item”属性,以便viewmodel可以从树视图中删除此节点。谢谢。目前,单击ContentPresenter
可选择TreeViewItem
,但不会切换复选框。您没有看到此行为,或者您希望它勾选/取消勾选复选框
?@dkozl我不希望它切换复选框。那么,您是否希望将树视图项
选定状态绑定到该树视图项
后面的视图模型的某些属性?基本上,如何定义“选定项”属性?
<DataTemplate DataType="{x:Type r:PrinterViewModel}">
<!--Bind the ContentControl to the DataType-->
<ContentControl Content="{Binding}"
ContentTemplate="{StaticResource sharedTemplate}" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<h:EventToCommand
Command="{Binding DataContext.SelectItem, RelativeSource={RelativeSource FindAncestor, AncestorType=TreeView}}"
CommandParameter="{Binding}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</ContentControl>
</DataTemplate>