Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF stop ListView ScrollBar firing click

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?

like image 361
sipsorcery Avatar asked Aug 27 '09 01:08

sipsorcery


1 Answers

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
}
like image 118
Jackson Pope Avatar answered Sep 21 '22 08:09

Jackson Pope