locked
How to set the selected grid item to move as the first top of the grid RRS feed

  • Question

  • hello
    I got a WPF Grid which got more than 100 rows and I got a search function which will set the selected item on the grid.how can i move the selected item on the grid to top of the grid i.e scoll up on the grid.
    Appreciate all your help
    Thursday, November 29, 2012 1:41 PM

Answers

  • The following is a sample of what you need to accomplish your goal:

    XAML:

    <Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
        <Grid>
            <TextBox Height="23" HorizontalAlignment="Left" Margin="15,10,0,0" Name="TextBox1" VerticalAlignment="Top" Width="120" />
            <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="165,6,0,0" Name="Button1" VerticalAlignment="Top" Width="75" Click="Button1_Click" />
            <DataGrid AutoGenerateColumns="True"  Height="249" HorizontalAlignment="Left" Margin="20,50,0,0" Name="DataGrid1" VerticalAlignment="Top" Width="471" />
        </Grid>
    </Window>
    

    Code behind:

    Class MainWindow 
    
        Public Property theData As New List(Of tmp)
    
        Private Sub Window_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs)
            For i As Integer = 0 To 99
                Dim t As New tmp With {.theValue = i}
                theData.Add(t)
            Next
            DataGrid1.ItemsSource = theData
        End Sub
    
        Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
            Dim idx As Integer = Integer.Parse(Me.TextBox1.Text)
    
            Dim sv As ScrollViewer = GetScrollViewer(DataGrid1)
            sv.ScrollToBottom()
            DataGrid1.ScrollIntoView(theData(idx))
        End Sub
    
        Public Shared Function GetScrollViewer(o As DependencyObject) As DependencyObject
            If TypeOf o Is ScrollViewer Then
                Return o
            End If
    
            For i As Integer = 0 To VisualTreeHelper.GetChildrenCount(o) - 1
                Dim child = VisualTreeHelper.GetChild(o, i)
    
                Dim result = GetScrollViewer(child)
                If result Is Nothing Then
                    Continue For
                Else
                    Return result
                End If
            Next
    
            Return Nothing
        End Function
    
    End Class
    
    Public Class tmp
        Public Property theValue As Integer
    End Class

    What it does is simple.  Just create a list of objects that can be displayed.  Into the textbox enter a value between 0 and 99.  Click the button and the datagrid item at the top should be the number you entered.  Kinda like a WPF magic trick.

    Hope this helps,

    Lloyd Sheen


    Lloyd Sheen

    • Proposed as answer by Pete LakerMVP Thursday, November 29, 2012 10:38 PM
    • Marked as answer by Rushdy Najath Friday, November 30, 2012 9:41 AM
    Thursday, November 29, 2012 10:35 PM