I have this ToggleButton
in my WP7 app which I bind to a property in my ViewModel. I also have a command to the ToggleButton
which does work when clicking the button.
Based on the result of that command, I set the property that is bound to the ToggleButton.IsChecked
property. But no matter what I set the property to, the toggle button lives its own life and just switches between unchecked and checked. Is this expected behaviour or is this a bug?
It seems like the toggle button loses its binding when clicking on it, would this be true? The reason I want it bound is that I do not always want to change the checked state, because the logic in my command can fail, e.g. network is down so it cant set what I want in the back end, and so forth.
Any workaround for this problem?
Xaml:
<ToggleButton x:Name="ToggleButton" Style="{StaticResource ToggleButtonStyle}" IsChecked="{Binding IsToggleButtonChecked}, Mode=OneWay}" Command="{Binding ToggleButtonCommand, Mode=OneWay}" CommandParameter="{Binding ToggleButtonCommandParameter}"/>
The style sets the image of the button based on states. The command does logic when the button is clicked and, as said earlier, sets IsToggleButtonChecked to desired value. I have both tried OneWay and TwoWay on the IsChecked, but I can´t see the difference.
ViewModel:
public const string IsToggleButtonCheckedPropertyName = "IsToggleButtonChecked";
private bool _isToggleButtonChecked;
public bool IsToggleButtonChecked
{
get { return _isToggleButtonChecked; }
set
{
if (_isToggleButtonChecked == value)
{
return;
}
_isToggleButtonChecked = value;
RaisePropertyChanged(IsToggleButtonCheckedPropertyName);
}
}
This property is set each time i want to change the checked state of the ToggleButton.