Telerik RadGridView in WPF: GetRowForItem method returns null after calling SortDescriptors.Reset()

StackOverflow https://stackoverflow.com/questions/18662057

Frage

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.

War es hilfreich?

Lösung

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());
        }));
    }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top