updating ToggleButton IsEnabled property throws exception

Topics: Conventions
Jan 30, 2014 at 10:07 PM
Edited Jan 30, 2014 at 10:12 PM

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 , 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:
            <ToggleButton x:Name="SayHello"  Grid.Column="1"  IsEnabled="{Binding HasValue}"  Height="190">
            <StackPanel x:Name="sp"> </StackPanel>
            <Button x:Name="Click"></Button>
the view model:

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

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

    public void Click()
        HasValue = !HasValue;

any way to workaround that one? - the platforms is WP8

Jan 31, 2014 at 5:25 PM
You do realize that you don't have to create a separate binding for that IsEnabled property right?

CanSayHello a bool property is looked for as part of the CM convention in the viewmodel. I suspect that as a result of the convention that it's looking for the Guard of SayHello (CanSayHello) and throwing a fit, that you bound IsEnabled and dying, I suggest a change in the naming of your property to CanSayHello and don't bind it to IsEnabled.

change HasValue name to CanSayHello....
Jan 31, 2014 at 6:38 PM
thanks for the reply.

yes I did realized that and desiged the test according to to your instructions in the first place.

however, the CanSayHello value had no effect for the ToggleButton IsEnabled state

Creating the Binding fixed that and the ToggleButton started to respond to the guard state

however, when i added a stack pannel to the ToggleButton, the exception described above was started a appear
Feb 1, 2014 at 6:57 PM
one thing I didn't do was name the nested stackpanel but unless you had another property with the "sp" name that was doing something weird I don't know. It should work with the guard, at least it did on my end and the togglebutton became active.