Search code examples
wpf

The BusyIndicator inside the Expander does not work


I want to create an Expander that contains a BusyIndicator and as soon as you click on the Expander, a BusyIndicator will appear until there is content

<Grid>
    <Expander Expanded="Expander_OnExpanded">
        <syncfusion:SfBusyIndicator x:Name="BusyIndicator">
            <ItemsControl x:Name="ItemsControl">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding}"/>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </syncfusion:SfBusyIndicator>
    </Expander>
</Grid>

private void Expander_OnExpanded(object sender, RoutedEventArgs e)
{
    BusyIndicator.IsBusy = true;
    ItemsControl.ItemsSource = new ObservableCollection<string> { "A", "B", "C" };
    Thread.Sleep(1000);
    BusyIndicator.IsBusy = false;
}

Solution

  • Thread.Sleep blocks the UI thread. Wait asynchronously or perform any long-running operation on a background thread:

    private async void Expander_OnExpanded(object sender, RoutedEventArgs e)
    {
        BusyIndicator.IsBusy = true;
        ItemsControl.ItemsSource = new ObservableCollection<string> { "A", "B", "C" };
        await Task.Delay(3000);
        BusyIndicator.IsBusy = false;
    }