8
votes

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?

3

3 Answers

4
votes

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.

8
votes

Neither of the other answers worked in my case because of complex styling in the ListBoxItem. This did however:

var item = ItemsControl.ContainerFromElement(sender as ItemsControl, (DependencyObject)e.OriginalSource) as ListBoxItem;
if (item != null)
{
    // Handle it
}
2
votes

For ListBox I've used the following code:

 if (e.OriginalSource is TextBlock || e.OriginalSource is Border)
 {
     // do your stuff
 }