Categories: Silverlight 3 Tips Posted on 7/16/2009 12:59 PM by Ryan Shelby  Feedback (2)

I've been having some trouble finding examples or documentation for making the mousewheel event happen in the Silverlight Datagrid.  I found it a bit complicated because the Datagrid's ScrollIntoView method depends on specifying an item inside the ItemSource

 

I found that if I created a derived datagrid class, I was able to access the RowPresenter from the Visual Tree containing all the datagridrows currently visible to the user.  I then can take the first and last row indexes and use these as a reference for scrolling to the previous or next rows inside the datagrid.

 

Imports System.Windows.Controls.Primitives
 
Public Class ScrollingDataGrid
    Inherits System.Windows.Controls.DataGrid
 
    Private m_RowPresenter As DataGridRowsPresenter
 
    Public Sub New()
        AddHandler Me.MouseWheel, AddressOf Grid_MouseWheel
    End Sub
 
    Public Overrides Sub OnApplyTemplate()
        MyBase.OnApplyTemplate()
        m_RowPresenter = TryCast(MyBase.GetTemplateChild("RowsPresenter"), DataGridRowsPresenter)
    End Sub
 
    Private Sub Grid_MouseWheel(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseWheelEventArgs)
 
        Me.ScrollIntoView(Me.ItemsSource(GetRowIndexToScrollTo(e.Delta)), Nothing)
        Me.UpdateLayout()
 
    End Sub
 
    Private Function GetRowIndexToScrollTo(ByVal _delta As Integer) As Integer
 
        'The row presenter children [datagridrows] are not stored in order by row index, so we need to
        'iterate though the children in order to find the highest and lowest datagrid row indexes currently visible.
 
        Dim highestRowIndex, lowestRowIndex As Integer
 
        For I As Integer = 0 To m_RowPresenter.Children.Count - 1
            Dim dgr As DataGridRow = m_RowPresenter.Children(I)
            If I = 0 Then lowestRowIndex = dgr.GetIndex
            If dgr.GetIndex < lowestRowIndex Then lowestRowIndex = dgr.GetIndex
            If dgr.GetIndex > highestRowIndex Then highestRowIndex = dgr.GetIndex
        Next I
 
        Return IIf(_delta < 0, highestRowIndex + 1, lowestRowIndex - 1)
 
    End Function
 
End Class

 

The derived class above only depends on the GetRowIndexToScrollTo function to return the next or previous row index based on the direction of the mouse wheel [positive or negative delta].

 

Anyway, is there some option in the datagrid like "EnableMouseWheel" that I happened to overlook?  I'm wondering why there isn't a mouse wheel default behavior built into the datagrid control...

 

Comments

Kiener
Kiener on 9/7/2009 4:29 AM Hi,

The UI Automation API provides a IScrollProvider interface. With this it is quite easy to scroll any scrollable region including the DataGrid.

Maybe these two examples can help you:

blog.thekieners.com/.../

or that one

blog.thekieners.com/.../

Best regards,
Beat Kiener
Morten
Morten on 9/18/2009 1:37 AM Check this approach out: www.sharpgis.net/.../...Silverlights-DataGrid.aspx

Send Feedback





biuquote
  • Comment
  • Preview
Loading