I have a MainWindow.xaml that has a user control and a ToggleButton:
<ToggleButton x:Name="toggle" Content="Wait" />
This button sets BusyDecorator User control property called IsBusyIndicatorShowing, it works as expected, whenever user clicks on toggLe button it sets user control property:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ctrls="clr-namespace:Controls"
Title="Busy" Height="300" Width="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="322*" />
<RowDefinition Height="53*" />
</Grid.RowDefinitions>
<ToggleButton x:Name="toggle" Content="Show" Margin="228,12,255,397" />
<ctrls:BusyDecorator HorizontalAlignment="Center" VerticalAlignment="Center" IsBusyIndicatorShowing="{Binding IsChecked, ElementName=toggle}">
<Image Name="canvas" Stretch="Fill" Margin="5" />
</ctrls:BusyDecorator>
</Grid>
</Window>
I want to bind BusyDecorator’s IsBusyIndicatorShowing property in code.
To do so I added IsBusyIndicatorShowing="{Binding IsBusyIndicatorShowing}" inside user control in xaml like
<ctrls:BusyDecorator HorizontalAlignment="Center" VerticalAlignment="Center" x:Name="Actions" IsBusyIndicatorShowing="{Binding IsBusyIndicatorShowing}">
...
But I do not know hot to define and set property inside code like
public bool doSomething()
{
//init
//toggle user control
BusyDecorator.IsBusyIndicatorShowing = true;
//do stuff
//toggle user control
BusyDecorator.IsBusyIndicatorShowing = false;
return true;
}
It does not work because it says
Error 2 An object reference is required for the non-static field, method, or property 'Controls.BusyDecorator.IsBusyIndicatorShowing.get'
Datacontext = this;I want to emulate the toggleButton Programatically, I mean, To delete it from xaml and emulate the action programatically to activate user controlDatacontext = this;is a huge code smell. Don't do that. Also, it wouldn't work anyhow, as your work is being done in the UI thread and so the UI wouldn't be updated until you are done. That's a common error.IsBusyIndicatorShowingproperty, and how to toggle it inside code?