Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将鼠标单击到HierarchycalDataTemplate中的ContentPresenter绑定到ViewModel?_C#_Wpf_Xaml_Mvvm - Fatal编程技术网

C# 如何将鼠标单击到HierarchycalDataTemplate中的ContentPresenter绑定到ViewModel?

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-->

我在树视图中使用了以下数据模板

如何让LeftMouseClick在ContentPresenter上以MVVM方式绑定到数据类型的属性

谢谢你的帮助(或更好的主意)


................
附录:这很有效

<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>