简体   繁体   English

如何在子用户控件的 combobox 中触发双击事件?

[英]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.由于UserControlControl的派生类,因此该事件可用并且可以处理。 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.

相关问题 双击WPF ListView-如何触发命令而不使用事件处理程序 - Double Click on a WPF ListView - how to fire a command and not use an event handler 自定义 Treeview 用户控件 MVVM 双击冒泡事件 WPF - Custom Treeview User Control MVVM Double Click Bubbling Event WPF 如何从用户控件中触发自定义事件? - How do I fire a custom event from a User Control? 如何将WPF中的命令绑定到控件的双击事件处理程序? - How to bind a command in WPF to a double click event handler of a control? WPF-如何激发ComboBox的MouseLeftButtonUp事件? - WPF - How to fire MouseLeftButtonUp event for ComboBox? 如何覆盖用户控件中的onTouchUp,它不仅可以用于触发此用户控件上的事件,还可以用于整个应用程序 - How to override the onTouchUp in the user control which can be used to fire the event not only on this user control, but whole application Fire父事件基于WPF中的子控件事件 - Fire Parent event based upon child control event in WPF WPF - Fire comboBox SelectedIndexChanged仅来自用户点击 - WPF - Fire comboBox SelectedIndexChanged only from user click 使用附加命令为列表视图触发双击事件-MVVM - using Attached Commands to fire a double click event for listview - MVVM 使用鼠标双击事件设置启用组合框 - Setting ComboBox enabled using double mouse click event
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM