0

I have a XAML listview defined thusly, with events defined on the "PreviewMouseRightButtonUp" and "PreviewMouseLeftButtonUp" events:

<ListView Name="lstItems" DockPanel.Dock="Top" >
        <ListView.ItemContainerStyle>
                <Style TargetType="{x:Type ListViewItem}" BasedOn="{StaticResource ListViewItemStyle}">
                    <EventSetter Event="PreviewMouseRightButtonUp"
                         Handler="OnListViewItemPreviewMouseRightButtonUp" />
                    <EventSetter Event="PreviewMouseLeftButtonUp"
                         Handler="OnListViewItemPreviewMouseLeftButtonUp" />
                </Style>
        </ListView.ItemContainerStyle>
</ListView>

I also have a class, ObjRow, that inherits from UserControl, with a button on it:

<DockPanel LastChildFill="True"> 
    <Button Name="btnOpen" DockPanel.Dock="Left" BorderBrush="Transparent" Style="{StaticResource FolderButton}" IsTabStop="False"/>
</DockPanel>

And finally, in the code-behind of the listview I call a method where I set an event on the button's "Click" event:

foreach (var obj_items in objs)
{
                ObjRow item = new ObjRow();
                item.btnOpen.Click += new RoutedEventHandler(btnOpen_Click);

                controls.Add(item);
}
lstItems.ItemsSource = controls;

Both events fire as expected. Previously, I had not had a "PreviewMouseLeftButtonUp" event, so left-clicking the button would only ever trigger the "Click" event. However, with that event added, left-clicking the button triggers both -- first the "PreviewMouseLeftButtonUp" from the listview, and then the "Click" from the UserControl.

I'm looking for a way to ignore the "Preview" event when the button is clicked, triggering only the "Click" event instead. Is there a way to make these disparate events play nice together? Or do I need to find another place to put the "PreviewMouseLeftButtonUp" code?

Raven Dreamer
  • 6,940
  • 13
  • 64
  • 101
  • What would you expect when someone clicks `Button` inside `ListView` while you have something (we don't know what) associated with tunneling mouse-click related events? You could use [bubbling](https://msdn.microsoft.com/en-us/library/ms742806(v=vs.110).aspx) events, then you shouldn't get `MouseUp` event if button were clicked. – Sinatr Nov 09 '16 at 15:12
  • [Some short and interesting read](http://stackoverflow.com/a/1460266/3283203) related to the subject. `Preview` is naturally called first, so maybe you'll want to replace it with the standard alternative. – Kilazur Nov 09 '16 at 15:32
  • @Sinatr Total WPF newbie, here -- that's exactly what I needed. Was not aware that each "Preview" event corresponded to an equivalent non-preview event. Thanks! – Raven Dreamer Nov 09 '16 at 18:39

1 Answers1

0

Some short and interesting read related to the subject.

Preview is naturally called before any actual event happens, so it will always be raised before Click.

Replacing PreviewMouseLeftButtonUp by MouseLeftButtonUp allows your Button.Click to act first.

Moreover, Button.Click will prevent the click event to get to MouseLeftButtonUp, which is in adequation with your requirement, but can be an issue if you actually need it to get to it. See this question to fix this behaviour if needed.

Community
  • 1
  • 1
Kilazur
  • 3,089
  • 1
  • 22
  • 48