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?

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top