0
votes

In a WPF MVVM solution, I am using a backgroundworker in order to show a busyindicator while fetching some data and populating a (telerik) treeview. I'm trying to do this when my (parent) usercontrol (holding the TreeView and the RadBusyIndicator) is loaded:

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
    BackgroundWorker bg = new BackgroundWorker();
            
    bg.DoWork += (object sender_, DoWorkEventArgs e_) =>
    {
        GetData();
    };

    bg.RunWorkerCompleted += (object sender_, RunWorkerCompletedEventArgs e_) =>
    {
         TreeView.ExpandAll();
         IsBusy = false;
    };

    IsBusy = true;
    bg.RunWorkerAsync();
}

The data fetching works just fine and the itemssource for the treeview is set normally. Everything works fine except for the TreeView.ExpandAll().

XAML code:

<telerik:RadTreeView x:Name="TreeView" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" SelectionMode="Single" ItemTemplate="{StaticResource OrderTemplate}" ItemsSource="{Binding Orders, Mode=OneWay}" SelectedItem="{Binding SelectedItem, Mode=OneWayToSource}" telerik:TreeViewPanel.TreeVirtualizationMode="Recycling" >

The top node of the treeview appears but the underlying items are not expanded. I have to click them open (that works fine). Any idea why the treeview is not expanding? What can I do to make this work?

1
You cannot create, access, or update any UI element from any thread other than the UI thread. You need to show us all of your code.Enigmativity

1 Answers

-1
votes

TreeView.ExpandAll() probably needs to be called from the UI thread. Try:

Application.Current.Dispatcher.Invoke(() => TreeView.ExpandAll());

That's assuming the BackgroundWorker wasn't created from the UI thread. If it was then seems like your code should work.