Sturm
Sturm

Reputation: 4125

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.

Upvotes: 5

Views: 6340

Answers (1)

Reed Copsey
Reed Copsey

Reputation: 564851

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}" />

Upvotes: 9

Related Questions