Closed MyZQL closed 2 months ago
Please provide a sample app that reproduces your scenario or this issue will be closed.
The example project is attached. I originally want to bind the "MyCommand" only on the "SelectionChanged" event of the TabControl, but when I change the selected item of anyone of the ComboBox "Combo1" and "Combo2", the "MyCommand" also will be invoked. That's not I want. Is it a bug? wpf-behavior-test.zip
This is not a bug. ComboBox and TabControl are derived from Selector, and the SelectionChanged event is a routed event, so the ComboBox.SelectionChanged
will be routed to parent control TabControl. This is the WPF routed event behavior.
You have a few options:
OriginalSource
private void MyTabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// Check if the event is raised by the TabControl itself
if (e.OriginalSource is TabControl)
{
// Execute your command here
var command = (DataContext as YourViewModel)?.MyCommand;
if (command != null && command.CanExecute(null))
{
command.Execute(null);
}
}
}
OriginalSource
Bug Description I just want to setup an EventTrigger for a TabControl to catch its SelectionChanged event. However, when I add some ComboBoxes to the TabItems, I find that the EventTrigger will be also triggered and invoke the command action, despite the SourceObject has been only set to the TabControl. Here is my Xaml code:
And the ViewModel code:
Expected behavior I only want to catch the SelectionChanged event of the TabControl, not including that of its child controls.