简体   繁体   English

WPF-如何激发ComboBox的MouseLeftButtonUp事件?

[英]WPF - How to fire MouseLeftButtonUp event for ComboBox?

I have a ComboBox who have to call a function that need the selectedItem of the ComboBox as parameter when the user select an item. 我有一个ComboBox,当用户选择一个项目时,他们必须调用一个需要ComboBox的selectedItem作为参数的函数。 Because this event need to be fired even when the item doesn't change I can't use the SelectionChanged event. 因为即使项目没有更改,也需要触发此事件,所以我无法使用SelectionChanged事件。 So to resolve this problem I wanted to use the MouseLeftButtonUp, but this event doesn't seems to work. 因此,要解决此问题,我想使用MouseLeftButtonUp,但此事件似乎不起作用。

I have tried to use the PreviewMouseLeftButtonUp event, who is triggered, but the selectedItem of the ComboBox is only modified after the event, wich is too late for me. 我尝试使用已触发的PreviewMouseLeftButtonUp事件,但是仅在事件之后才修改ComboBox的selectedItem,这对我来说太迟了。

I have also tried the MouseLeftButtonDown event, but it's doesn't work either. 我也尝试了MouseLeftButtonDown事件,但是它也不起作用。

WPF : WPF:

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <ComboBox x:Name="cb" VerticalAlignment="Top" HorizontalAlignment="Left" IsHitTestVisible="True"
                  PreviewMouseLeftButtonUp="Cb_PreviewMouseLeftButtonUp"
                  MouseLeftButtonUp="Cb_MouseLeftButtonUp"
                  MouseLeftButtonDown="Cb_MouseLeftButtonDown"
                  SelectionChanged="Cb_SelectionChanged"/>
    </Grid>
</Window>

C# for testing : 用于测试的C#:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace WpfApp1 {
    public partial class MainWindow : Window {
        public MainWindow() {
            InitializeComponent();
            cb.Items.Add("a");
            cb.Items.Add("b");
        }
        private void Cb_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e) {
            Console.WriteLine("event : Preview mouse UP");
        }
        private void Cb_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) {
            Console.WriteLine("event : Mouse UP"); // Does't fire
        }
        private void Cb_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
            Console.WriteLine("event : Mouse DOWN"); // Does't fire either
        }
        private void Cb_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            Console.WriteLine("event : selection changed"); // Only fire if the selected item change
        }
    }
}

So basically I just want to know if it's possible to trigger the MouseLeftButtonUp event. 所以基本上我只想知道是否有可能触发MouseLeftButtonUp事件。

即使选择了已选择的元素,也会触发DropDownClosed

Thanks to mami , I have found the solution : 多亏了妈妈 ,我找到了解决方法:

this.AddHandler(
    ComboBox.MouseLeftButtonUpEvent,
    new MouseButtonEventHandler(Cb_MouseLeftButtonUp),
    true
);

More info here 更多信息在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM