Question

The GetRowForItem method returns null after calling grid.SortDescriptors.Reset(). The following code is an example of a button click event that gets the row for a selected item:

    private void GetRowForSelectedItem_Click(object sender, RoutedEventArgs e)
    {
        this.clubsGrid.SortDescriptors.Reset();
        var r = this.clubsGrid.GetRowForItem(this.clubsGrid.SelectedItem);
        MessageBox.Show(r.ToString());
    }

The value of r is null.

Was it helpful?

Solution

The reason this is occurring is the Grid.SortDescriptors.Reset() method is executing on the UI thread and hasn't finished by the time the GetRowForItem method is called. Here is a workaround that seems to work well for me. I'm calling the SortDescriptors.Reset() method, and then calling BeginInvoke (with DispatcherPriority of Input) to make the call to the GetRowForItem method.

 private void GetRowForSelectedItem_Click(object sender, RoutedEventArgs e)
    {
        this.clubsGrid.SortDescriptors.Reset();

        System.Windows.Threading.Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Input, new Action(() =>
        {
            var r = this.clubsGrid.GetRowForItem(this.clubsGrid.SelectedItem);
            MessageBox.Show(r.ToString());
        }));
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top