Bind ToggleButton to a bool
Asked Answered
P

1

5

I'm trying to implement a toggle button that permits the user to select between linear or logarithmic axis.

For that I have in my View this ToggleButton:

<ToggleButton Width="40" Height="20" Margin="2" Grid.Row="1" Content="LogX" VerticalAlignment="Center" IsChecked="{Binding LogXChecked, Mode=TwoWay}"/>

In my ViewModel:

private bool _isLogXChecked;
public bool IsLogXChecked
{
    get
    {
        return _isLogXChecked;
    }
    set
    {
        _isLogXChecked = value;
        RaisePropertyChanged("IsLogXChecked");
        LogX();
    }
}

But with this implementation I can't get it to work, the IsLogXChecked property does not update when the user presses the ToggleButton, and my method LogX() does not fire.

Where might be the problem? Or how should I bind a ToggleButton to a bool? Thank you.

Pretonic answered 24/2, 2014 at 22:45 Comment(0)
S
9

Your XAML binds to LogXChecked, but your ViewModel defines this as IsLogXChecked. Right now, the binding is broken because the property name doesn't match the binding specification.

You can fix this on either side - for example, fix this in the Xaml via:

 <ToggleButton Width="40" Height="20" Margin="2" 
    Grid.Row="1" Content="LogX" VerticalAlignment="Center" 
    IsChecked="{Binding IsLogXChecked, Mode=TwoWay}" />
Statampere answered 24/2, 2014 at 22:47 Comment(2)
Oh my god, should I delete this question? Thank you very much Reed.Pretonic
@Pretonic Probably not - it's a common enough, real world issue ;)Statampere

© 2022 - 2024 — McMap. All rights reserved.