我在 MVVM 场景中使用 TreeView。由于子 TreeViewItem 的显示和上下文菜单取决于视图模型的类型,因此我使用数据模板来选择要显示的正确 UserControl(比 StyleSelector 更容易管理)。
我的问题是,当在其表面上的任何位置单击 UserControl 时,我需要处理命令。我使用了直接附加到 UserControl 的 EventTrigger,但只有当我单击TextBlock 或图像的文本时才会处理单击事件。这里有一个示例代码:
<UserControl x:Class="FolderTreeView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonDown">
<i:InvokeCommandAction Command="{Binding Path=DisplayCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Path=Icon}"/>
<TextBlock Text="{Binding Path=DisplayName}"/>
</StackPanel>
</UserControl>
关于我可以让它工作的任何想法?