WPF ContentControl BusyIndicator

Gerd Canori 1 Reputation point
2022-02-17T09:25:08.057+00:00

s there a way to display the busy indicator in the content control during the content change?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,756 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Gerd Canori 1 Reputation point
    2022-02-21T09:04:53.677+00:00

    Like this example:
    https://doc.xceed.com/xceed-toolkit-plus-for-wpf/Xceed.Wpf.Toolkit~Xceed.Wpf.Toolkit.BusyIndicator~BusyContentTemplate.html
    in combination with MVVM.

    The content and the busy indicator always displayed after the content is displayed. Not between a content change.

    Do you have a working sample ?

    regards

    0 comments No comments

  2. Hui Liu-MSFT 48,256 Reputation points Microsoft Vendor
    2022-02-22T09:00:22.517+00:00

    You could try to refer to the code below.
    Add Extended.Wpf.Toolkit package reference.
    MainWindow.xaml:

    <Window x:Class="ContentControlDemo.MainWindow"  
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
            xmlns:local="clr-namespace:ContentControlDemo"  
            mc:Ignorable="d"  
            xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"  
            Title="MainWindow" Height="450" Width="800">  
        <xctk:BusyIndicator x:Name="AutomationIndicator">  
            <xctk:BusyIndicator.BusyContentTemplate>  
                <DataTemplate>  
                    <StackPanel Margin="4">  
                        <TextBlock Text="Sending Invoices" FontWeight="Bold" HorizontalAlignment="Center"/>  
                        <WrapPanel>  
                            <TextBlock Text="Items remaining: "/>  
                            <TextBlock x:Name="_ItemsRemaining" Text="{Binding Path=ItemsRemaining, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}"/>  
                        </WrapPanel>  
                    </StackPanel>  
                </DataTemplate>  
            </xctk:BusyIndicator.BusyContentTemplate>  
            <StackPanel>  
                 
                <Button x:Name="btn" Content="Switch" Click="btn_Click" Width="200" Margin="5"/>  
                <local:UserControl1 DataContext="{Binding CurrentVM}"/>  
            </StackPanel>  
        </xctk:BusyIndicator>  
    </Window>  
    

    UserControl:

     <UserControl.Resources>  
            <DataTemplate DataType="{x:Type local:MyViewModel1}">  
                <local:MyViewModel1View DataContext="{Binding}"/>  
            </DataTemplate>  
            <DataTemplate DataType="{x:Type local:MyViewModel2}">  
                <local:MyViewModel2View DataContext="{Binding}"/>  
            </DataTemplate>  
        </UserControl.Resources>  
        <ContentControl Content="{Binding}"/>  
    

    UserControl.xaml.cs:

    public partial class UserControl1 : UserControl  
      {  
        public UserControl1()  
        {  
          InitializeComponent();  
        }  
      }  
      public class MyViewModel1  
      {  
        public string Info1 { get; set; }  
      }  
      public class MyViewModel2  
      {  
        public string Info2 { get; set; }  
      }  
      public class MyViewModel1View : UserControl  
      {  
        public MyViewModel1View()  
        {  
          StackPanel stp = new StackPanel();  
          this.Content = stp;  
          stp.Children.Add(new Label() { Content = "1. View" });  
          Label lbl = new Label();  
          lbl.SetBinding(Label.ContentProperty, new Binding("Info1"));  
          stp.Children.Add(lbl);  
        }  
      }  
      public class MyViewModel2View : UserControl  
      {  
        public MyViewModel2View()  
        {  
          StackPanel stp = new StackPanel();  
          this.Content = stp;  
          stp.Children.Add(new Label() { Content = "2. View" });  
          Label lbl = new Label();  
          lbl.SetBinding(Label.ContentProperty, new Binding("Info2"));  
          stp.Children.Add(lbl);  
        }  
      }  
    

    MainWindow.xaml.cs:

    public partial class MainWindow : Window,INotifyPropertyChanged  
      {  
        public object CurrentVM { get; set; } = new MyViewModel1();  
        public MainWindow()  
        {  
          InitializeComponent();  
          DataContext=this;  
        }  
        public void UpdateItemsRemaining(int n)  
        {  
          this.ItemsRemaining = n.ToString();  
        }  
        private void btn_Click(object sender, RoutedEventArgs e)  
        {  
          TestStatus();  
        }  
        private string _itemsRemaining;  
        public string ItemsRemaining  
        {  
          get { return _itemsRemaining; }  
          set { _itemsRemaining = value; OnPropertyChanged(); }  
        }  
        public event PropertyChangedEventHandler PropertyChanged;  
        private void OnPropertyChanged([CallerMemberName] string propName = "") =>  
          PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));  
      }  
    

    The result:
    176689-12.gif


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.