Results 1 to 5 of 5

Thread: Preventing a DataGrid from eating mouse wheel events

  1. #1

    Thread Starter
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Preventing a DataGrid from eating mouse wheel events

    I have a ScrollViewer wrapped around an ItemsControl which is bound to a collection of mine. I've made a DataTemplate for the items in the ItemsControl. The DataTemplate includes a DataGrid. Everything works well except that I can't effectively use mouse wheel scrolling. The DataGrids don't need to be scrolled individually and are sized so that all of their content displays at once. There's usually too many items to display in one screen, hence the overall ScrollViewer. Whenever I use the mouse wheel over the DataGrids the events are eaten without effectively doing anything. What I want is for mouse wheel input to be processed by the overall ScrollViewer. Using the mouse over other parts of an item's template works fine, the only trouble is with the DataGrid part.

    So, how do I either prevent the DataGrids from processing mouse wheel events or route these events to the overall ScrollViewer?


    To be clear, my arrangement is approximately
    Code:
    <ScrollViewer>
      <ItemsControl ItemsSource="{Binding ...}">
        <ItemsControl.ItemTemplate>
          <DataTemplate>
            <DataGrid ...>
              ...
            </DataGrid>
          </DataTemplate>
        </ItemsControl.ItemTemplate>
      </ItemsControl>
    </ScrollViewer>
    The template is actually more complex, as is the rest, but these should be the important bits.


    Thanks for any input!
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  2. #2
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Preventing a DataGrid from eating mouse wheel events

    Essentially, the DataGrid is providing all this scrolling ability for you because it's designed to hold some unknown amount of data and provide a viewport onto it. If you have a known quantity of data and have already made the grid big enough and explicitly don't want scrolling in the grid, is DataGrid actually the best control to use? Would a simple Grid be sufficient?

    I wondered whether you might be able to do it by handling the MouseWheel events of the DataGrid and explicitly marking the Handled property of the MouseWheelEventArgs to false, but that seemed to have no effect in my 2 minute test.

  3. #3

    Thread Starter
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: Preventing a DataGrid from eating mouse wheel events

    A Grid might be sufficient, but then I'd be reimplementing functionality by manually creating the column headers and textboxes myself. It's a good workaround, but I'd like to avoid it. I'd be happy using a similar existing element if one would be more appropriate. DataGrids also allow selection, which I don't exactly need, though it might be nice in the future. Other than selection and scrolling, though, I'm very happy with how the UI turned out.

    I'm not well-versed in styling UI elements whatsoever--is there a way to just remove the ScrollViewer from inside the DataGrid? Alternatively, on a DataGrid MouseWheel event, can I manually raise the ScrollViewer's MouseWheel event? I'm fine with the original event doing nothing in the background while a copy is sent off to do something useful, even though it's somewhat ugly.

    None of this would be an issue if the ScrollViewer did what was reasonable--on mouse wheels that cause no scrolling, don't handle the event....
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  4. #4
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Preventing a DataGrid from eating mouse wheel events

    If you have a copy of Expression Blend, you can get the default Control Template for a DataGrid. Drop a DataGrid on to your document, and then you can get a copy of its template into the document as a resource (accessed from the context menu on the document explorer). From there, you can inspect the template, see if you can find what's providing the scrolling and extract it without completely breaking everything. If you can do that, then you can put the modified template in your project as a resource and make the DataGrids on your form use that Template instead of the default one.

  5. #5

    Thread Starter
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: Preventing a DataGrid from eating mouse wheel events

    Unfortunately I don't have Expression Blend. Perhaps this page lists what I would have gotten out of it? It seems like the DataGrid innards are just wrapped inside a ScrollViewer, but I wasn't able to find any reference to such a thing, or any other scrolling mechanism besides a vertical and horizontal scroll bar. Again, I have almost no experience with styling / UI element creation, so I'd be very unsurprised if I missed something.

    I also found another thread discussing this issue where the most relevant reply is "[t]his is an issue with the WPF ScrollViewer, which marks all mouse wheel messages as handled. We are tracking this issue in our internal database." I suppose I can hope for the issue to be resolved in some future release, though that's not helpful now.


    Is there a way to manually raise the MouseWheel event of the outer ScrollViewer?
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width