1

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'   
5
  • Could you give a further explanation what do yo want to do? Because I do not understand your question. Commented Aug 19, 2015 at 13:50
  • What exactly do you want to do inside the method doSomething? And in which context is it executed? Commented Aug 19, 2015 at 14:16
  • Datacontext = this; I want to emulate the toggleButton Programatically, I mean, To delete it from xaml and emulate the action programatically to activate user control Commented Aug 19, 2015 at 14:20
  • Datacontext = 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. Commented Aug 19, 2015 at 14:32
  • So How do I define user control IsBusyIndicatorShowing property, and how to toggle it inside code? Commented Aug 19, 2015 at 14:37

1 Answer 1

1

The error message is the key to your problem, assuming I understand your question correctly. When you say "BusyDecorator.IsBusyIndicatorShowing = true" you are using the BusyDecorator class definition (as though it is static), not the instance you have defined in your XAML.

You should be able to name your XAML instance (note the x:Name):

<ctrls:BusyDecorator x:Name="myBusyDecorator" HorizontalAlignment="Center" VerticalAlignment="Center" IsBusyIndicatorShowing="{Binding IsChecked, ElementName=toggle}">
            <Image  Name="canvas" Stretch="Fill"  Margin="5" />
         </ctrls:BusyDecorator>

Then you should be able to refer to that instance in code and access the property in whatever event you desire as such:

myBusyDecorator.IsBusyIndicatorShowing = true;
Sign up to request clarification or add additional context in comments.

1 Comment

Damn it was that easy, Thank you so much Bobby.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.