处理整个 UserControl 表面上的单击事件
我在 MVVM 场景中使用 TreeView。由于子 TreeViewItem 的显示和上下文菜单取决于视图模型的类型,因此我使用数据模板来选择要显示的正确 UserControl(比 StyleSelector 更容易管理)。
我的问题是,当用户控件被单击表面上的任何位置时,我需要处理命令。我使用直接附加到 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>
有什么想法可以让这个工作吗?
I am using a TreeView in a MVVM scenario. As the display and context menu of children TreeViewItem depends on the type of view-model, I am using data-templates to select the right UserControl to display (much easier to manage than StyleSelector).
My problem is that I need to handle commands when the UserControl is clicked anywhere on its surface. I used EventTrigger directly attached to the UserControl but the click event is handled only when i click on the text of the TextBlock or the Image. Here a sample code:
<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>
Any idea on I could get this to work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为您的
UserControl
指定背景颜色。默认情况下,背景颜色为透明
,这意味着命中测试会直接穿过它Give your
UserControl
a background color. By default, the background color isTransparent
, which means hit tests fall right through it