Simply put, the ListViewItem.MouseDoubleClick
event is not a RoutedEvent
like the ButtonBase.Click
event. It uses the MouseButtonEventHandler
delegate and a Direct
routing strategy where as the ButtonBase.Click
event is a RoutedEvent
with a Bubbling
strategy. They are different types of events and are implemented differently.
WPF RoutedEvent inconsistency: ListViewItem.MouseDoubleClick Not bubbling correct e.Source
-
03-06-2022 - |
Question
Binding a RoutedEvent for the "Click" event of static buttons contained in a StackPanel is easy. The RoutedEventArgs will contain the button as the e.Source of the event.
XAML:
<StackPanel Grid.Column="1" Button.Click="RoutedEventHandler">
<Button Name="btn1" Content="btn1" />
<Button Name="btn2" Content="btn2" />
</StackPanel>
Code Behind:
private void RoutedEventHandler(object sender, RoutedEventArgs e)
{
MessageBox.Show(((FrameworkElement)e.Source).Name);
}
However - handling routed events with "ListViewItem.MouseDoubleClick" will result the ListView container object in the e.Source, instead of the expected ListViewItem object.
XAML:
<ListView Name="lbAnecdotes"
ListViewItem.MouseDoubleClick="RoutedEventHandler">
<ListView.ItemTemplate >
<DataTemplate>
<WrapPanel >
<TextBlock Text="{Binding Path=Name}" />
</WrapPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView >
Can you explain the inconsistency?
La solution
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow