WPF - 如何触发 ComboBox 的 MouseLeftButtonUp 事件?
WPF - How to fire MouseLeftButtonUp event for ComboBox?
我有一个 ComboBox,当用户 select 一个项目时,它必须调用一个需要 ComboBox 的 selectedItem 作为参数的函数。
因为即使项目没有更改也需要触发此事件,所以我无法使用 SelectionChanged 事件。所以为了解决这个问题,我想使用 MouseLeftButtonUp,但是这个事件似乎不起作用。
我尝试使用触发的 PreviewMouseLeftButtonUp 事件,但是 ComboBox 的 selectedItem 仅在事件发生后才被修改,这对我来说太晚了。
我也尝试过 MouseLeftButtonDown 事件,但它也不起作用。
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# :
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
}
}
}
所以基本上我只想知道是否可以触发 MouseLeftButtonUp 事件。
DropDownClosed 即使您 select 已经 selected 的元素也会触发。
感谢mami,我找到了解决方案:
this.AddHandler(
ComboBox.MouseLeftButtonUpEvent,
new MouseButtonEventHandler(Cb_MouseLeftButtonUp),
true
);
更多信息here
我有一个 ComboBox,当用户 select 一个项目时,它必须调用一个需要 ComboBox 的 selectedItem 作为参数的函数。 因为即使项目没有更改也需要触发此事件,所以我无法使用 SelectionChanged 事件。所以为了解决这个问题,我想使用 MouseLeftButtonUp,但是这个事件似乎不起作用。
我尝试使用触发的 PreviewMouseLeftButtonUp 事件,但是 ComboBox 的 selectedItem 仅在事件发生后才被修改,这对我来说太晚了。
我也尝试过 MouseLeftButtonDown 事件,但它也不起作用。
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# :
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
}
}
}
所以基本上我只想知道是否可以触发 MouseLeftButtonUp 事件。
DropDownClosed 即使您 select 已经 selected 的元素也会触发。
感谢mami,我找到了解决方案:
this.AddHandler(
ComboBox.MouseLeftButtonUpEvent,
new MouseButtonEventHandler(Cb_MouseLeftButtonUp),
true
);
更多信息here