R – WPF stop ListView ScrollBar firing click

listviewscrollbarwpf

I am using a WPF ListView with an always visible vertical scrollbar. I have a MouseLeftButtonUp event handler on the ListView. The handler is working properly except when the vertical scrollbar is clicked when it has nothing to do i.e. the ListView box doesn't have enough items to do any scrolling.

In that case nothing should happen as the user has clicked on the vertical scrollbar just to make sure there are no items just off the screen. However the ListView fires the MouseLeftButtonUp event. If the vertical scrollbar does have some work to do the event does not get fired.

Here is my simplifiewd XAML

<ListView MouseLeftButtonUp="DoSomething_MouseLeftButtonUp" SelectionMode="Single" ScrollViewer.VerticalScrollBarVisibility="Visible">
  <ListView.View>
    <GridView>
      <GridViewColumn Width="170" Header="Venue" DisplayMemberBinding="{Binding Path=Venue}" />
    </GridView>
  </ListView.View>
</ListView>

Is there anyway to prevent the MouseLeftButtonUp event firing when the vertical scroll bar is clicked irespective of whether the scroll bar has any work to do or not?

Best Answer

This is similar to this question, and the answer is the same. In your MouseLeftButtonUp handler, check the MouseButtonEventArgs.OriginalSource property. That will tell you where the click originated.