ofer
ofer

Reputation: 4476

can't bind to IsEnabled property of ToggleButton using caliburn micro

I'm trying to bind the IsEnabled property of a ToggleButton with no success.

once the NotifyOfPropertyChange is fired, I'm getting the following exception:

Value does not fall within the expected range.

Using a simple Button, the above configurations works as expected.

I wonder if there any workaround for that one?

Thanks

UPDATE:

well it took me a while to pinpoint the problem, but finally managed to understand the behavior:

I've created a simple tester where I use a button to enable/disable a ToggleButton.

when the ToggleButton control does not contain anything, all works properly; however, after adding sub controls to it (in our case I just added a StackPanel) an exception is raised:

Value does not fall within the expected range - right after NotifyOfPropertyChange() is called.

Here is the problematic view I'm using:

<StackPanel>
    <ToggleButton x:Name="SayHello"  Grid.Column="1"  IsEnabled="{Binding HasValue}"  Height="190">
        <StackPanel x:Name="sp"> </StackPanel>
    </ToggleButton>
    <Button x:Name="Click"></Button>
</StackPanel>

The ViewModel:

    private bool _hasvalue;

    public bool HasValue
    {
        get { return _hasvalue; }
        set
        {

                _hasvalue = value;
                NotifyOfPropertyChange(() => HasValue);
        }
    }

    public void Click()
    {
        HasValue = !HasValue;
    }

Any way to workaround that one? - the platforms is WP8.

Upvotes: 2

Views: 2216

Answers (1)

Chris
Chris

Reputation: 8656

I couldn't replicate the error from the example above, is there additional information in your ViewModel?

you should also be able to get the effect you want (although I'd still be interested to see the root cause of your error), by using the Caliburn.Micro conventions. Is x:Name=sp causing anything to be bound?

If you have a method SayHello, with a UI element bound to the method via a convention: x:Name="SayHello"

You can create a bool property on your ViewModel called CanSayHello, which Caliburn.Micro will use to Enable/Disable the control; although you will have to call NotifyPropertyChanged when that property changes (so the UI is aware and can update the control).

E.g.

<!-- Your existing Control, Note `IsEnabled` is not bound -->
<ToggleButton x:Name="SayHello" Height="40">

// On your ViewModel
public bool CanSayHello
{
    get
    {
        return HasValue;
    }
}

public void Click()
{
    HasValue = !HasValue;
    NotifyOfPropertyChange(() => CanSayHello);
}

Some additional info.

Upvotes: 1

Related Questions