Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind ToggleButton to a bool

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.

like image 473
Sturm Avatar asked Feb 24 '14 22:02

Sturm


1 Answers

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}" />
like image 193
Reed Copsey Avatar answered Sep 27 '22 22:09

Reed Copsey