[英]How to fire double click event in a combobox in a child user control?
I have a user control that use another user control.我有一个使用另一个用户控件的用户控件。 The child user control has a combobox and the click event works because i can open the combobox, but the double click it doesn't work.
子用户控件有一个 combobox 并且单击事件有效,因为我可以打开 combobox,但双击它不起作用。
My code is this:我的代码是这样的:
Main user control:主要用户控制:
<StackPanel>
<views:ucMyChildUserControl/>
</StackPanel>
My child user control:我的孩子用户控件:
<StackPanelOrientation="Horizontal">
<StackPanel Orientation="Vertical">
<Label Content="Content" Style="{StaticResource LabelDefault}"/>
<ComboBox Name="cmbMyCombobox"/>
</StackPanel>
<!--More related controls-->
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<i:InvokeCommandAction Command="{Binding MyCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</StackPanel>
But I have realized that if the comand of mouse double click is set in the parent user control, it works:但是我已经意识到,如果在父用户控件中设置了鼠标双击的命令,它就可以工作:
<views:ucChildUserControl>
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<i:InvokeCommandAction Command="{Binding MyCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</views:ucChildUserControl>
So I guess the problem is about the handling of the event, but I don't know how to catch it in the child user control.所以我猜问题是关于事件的处理,但我不知道如何在子用户控件中捕获它。
Thanks.谢谢。
The issue is that the MouseDoubleClick
event and the PreviewMouseDoubleClick
are defined on Control
.问题是
MouseDoubleClick
事件和PreviewMouseDoubleClick
是在Control
上定义的。 Since UserControl
is a derivative of Control
, the event is available and can be handled.由于
UserControl
是Control
的派生类,因此该事件可用并且可以处理。 However, StackPanel
is not a derivative of Control
so the event is not available, therefore the trigger does not work.但是,
StackPanel
不是Control
的派生,因此该事件不可用,因此触发器不起作用。
There are workarounds to this in code-behind that you can eventually turn into a behavior for MVVM, but simple workarounds like using input bindings on the left mouse click action only work on some elements, as others like ComboBox
will already handle it and then the input bindings are not triggered .在代码隐藏中有一些解决方法,您最终可以变成 MVVM 的行为,但是简单的解决方法(例如在鼠标左键单击操作上使用输入绑定)仅适用于某些元素,因为
ComboBox
等其他元素已经处理它,然后不触发输入绑定。
<StackPanel Orientation="Horizontal">
<StackPanel.InputBindings>
<MouseBinding MouseAction="LeftDoubleClick"
Command="{Binding MyCommand}"/>
</StackPanel.InputBindings>
<StackPanel Orientation="Vertical">
<Label Content="Content"/>
<ComboBox Name="cmbMyCombobox"/>
</StackPanel>
<!--More related controls-->
</StackPanel>
The most simple solution without creating additional code is to wrap the StackPanel
in a control that is a derivative of Control
, eg a ContentControl
, like this.无需创建额外代码的最简单的解决方案是将
StackPanel
包装在 Control 派生的Control
中,例如ContentControl
,就像这样。
<ContentControl>
<StackPanel Orientation="Horizontal">
<StackPanel Orientation="Vertical">
<Label Content="Content"/>
<ComboBox Name="cmbMyCombobox"/>
</StackPanel>
<!--More related controls-->
</StackPanel>
<b:Interaction.Triggers>
<b:EventTrigger EventName="MouseDoubleClick">
<b:InvokeCommandAction Command="{Binding MyCommand}" />
</b:EventTrigger>
</b:Interaction.Triggers>
</ContentControl>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.