C # WPF DataGrid ScrollIntoView not working with touch

I am trying to scroll to the last selected item in WPF DataGrid

in an event Loaded

. DataGrid

located in Tab

. Everything works when I test it in a normal Windows environment. But as soon as I touch the TabPage on the tablet instead of pressing it, it doesn't scroll to my last selected item. This is my code:

private void dataGrid_Loaded(object sender, RoutedEventArgs e)
{
    var currentItem = dataGrid.SelectedItem;

    dataGrid.ItemsSource = sh.GetDataTable(<SQL Select statement>).DefaultView;

    if (!(currentItem == null))
    {
        dataGrid.ScrollIntoView(currentItem);
    }
}

      

I also tried the solution I found here , but it didn't work.

Edit:

For testing purposes, I removed the event completely dataGrid_Loaded

. Now I only load data DataGrid

at the beginning of the program. Even now, it retains the scroll position when I switch between tabs with mouse clicks, but not touch! Is this a bug in the .NET Framework?

+3


source to share


1 answer


With the help of the MSDN community, I was able to resolve the issue.

I needed to scroll to the end DataGrid

, do UpdateLayout()

and then scroll to Item

which I want. Also, I can't just install ItemsSource

every time because the previously saved Item

one was not valid Item

for DataGrid

.

So my method dataGrid_Loaded

looks like this:



private void dataGrid_Loaded(object sender, RoutedEventArgs e)
{
    object currentPos = dataGrid.SelectedItem;

    if (dataGrid.ItemsSource == null)
    {
        dataGrid.ItemsSource = sh.GetDataTable("<SQL query>").DefaultView;
    }
    else
    {
        dataGrid.Items.Refresh();
    }

    if (currentPos != null)
    {
        dataGrid.ScrollIntoView(dataGrid.Items[dataGrid.Items.Count - 1]);
        dataGrid.UpdateLayout();
        dataGrid.ScrollIntoView(currentPos);
    }
}

      

I hope this helps someone else who has the same problem.

For reference here is my German MSDN thread which solved my problem.

+2


source







All Articles