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. Because this event need to be fired even when the item doesn't change I can't use the SelectionChanged event. So to resolve this problem I wanted to use the MouseLeftButtonUp, but this event doesn't seems to work.
I have tried to use the PreviewMouseLeftButtonUp event, who is triggered, but the selectedItem of the ComboBox is only modified after the event, which is too late for me.
I have also tried the MouseLeftButtonDown event, but it's doesn't work either.
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 :
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"); // Doesn't fire
}
private void Cb_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
Console.WriteLine("event : Mouse DOWN"); // Doesn'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.
Thanks to mami, I have found the solution :
this.AddHandler(
ComboBox.MouseLeftButtonUpEvent,
new MouseButtonEventHandler(Cb_MouseLeftButtonUp),
true
);
More info here