Search code examples
c#wpfwinformsbusyindicator

How to start my BusyIndicator from code behind


I have this User control: I added this user control to my Winforms application (simple BusyIndicator):

<UserControl x:Class="MyApp.UserControl1"             
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:xctk="clr-namespace:Xceed.Wpf.Toolkit;assembly=Xceed.Wpf.Toolkit"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <xctk:BusyIndicator x:Name="busyIndicator" IsBusy="{Binding IsBusy}"  />
    </Grid>
</UserControl>



   public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        public void SetIndicator(bool isBusy)
        {
            busyIndicator.IsBusy = isBusy;
        }
    }

Now from my main form i try to start my BusyIndocator:

UserControl1 uc = new UserControl1();
uc.SetIndicator(true);

Also if i am changing IsBusy="{Binding IsBusy}" into IsBusy="true i can see my BusyIndicator but then i cannot stop it.

But nothing happening, what i am doing wrong ?


Solution

  • Don't use data binding since you are not familiar with WPF, use code to set the value of IsBusy.

    <xctk:BusyIndicator x:Name="busyIndicator" /> //remove IsBusy="{Binding IsBusy}"
    

    In your Form, define an instance of UserControl1 and give it a name uc;

    public partial class MyForm : Form
    {        
        UserControl1 uc; //this is the "instance".
        public MyForm ()
        {
            InitializeComponent();
            uc = new UserControl1();
            uc.SetIndicator(true); //start the busy indicator
            this.elementHost1.Child = uc;
        }
    }
    

    And if you want to stop it, call

    uc.SetIndicator(false); //stop the busy indicator