WPF ToggleButton IsChecked binding issue
data-binding, togglebutton, wpf
Solution
The problem is just that you're raising the property change notification prior to actually changing the property value. Therefore, WPF reads the old value of the property, not the new. Change to this:
public bool? IsPermanentFailureState
{
get { return isPermFailure; }
set
{
if (isPermFailure != value.Value)
{
isPermFailure = value.Value;
NotifyPropertyChanged("IsPermanentFailureState");
}
if (isPermFailure) IsTransitoryFailureState = false;
}
}
public bool? IsTransitoryFailureState
{
get { return isTransitoryFailureState; }
set
{
if (isTransitoryFailureState != value.Value)
{
isTransitoryFailureState = value.Value;
NotifyPropertyChanged("IsTransitoryFailureState");
}
if (isTransitoryFailureState) IsPermanentFailureState = false;
}
}
Incidentally, you say it works when you use the interface rather than code, but I can't see it possibly could.
Problem
I am trying to create a situation where one or none of a grouping of two `ToggleButton`s can be on at any time. The problem that I have is if I change the state of the backing variable the UI state does not update. I do have `INotifyPropertyChanged` implemented. I've created my `ToggleButton`s like this: ``` <ToggleButton IsChecked="{Binding Path=IsPermanentFailureState, Mode=TwoWay}" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"> <TextBlock TextWrapping="Wrap" TextAlignment="Center">Permanent Failure</TextBlock> </ToggleButton> <ToggleButton IsChecked="{Binding Path=IsTransitoryFailureState, Mode=TwoWay}" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"> <TextBlock TextWrapping="Wrap" TextAlignment="Center">Temporary Failure</TextBlock> </ToggleButton> ``` This is my backing properties (I am using the MVVM pattern, the other bindings work, IE clicking on the `ToggleButton` does enter these property settings. When I'm changing the state via code, the Toggle Buttons don't change visual state. IE I am setting the backing property to false, but the button stays checked. ``` public bool? IsPermanentFailureState { get { return isPermFailure; } set { if (isPermFailure != value.Value) { NotifyPropertyChanged("IsPermanentFailureState"); } isPermFailure = value.Value; if (isPermFailure) IsTransitoryFailureState = false; } } public bool? IsTransitoryFailureState { get { return isTransitoryFailureState; } set { if (isTransitoryFailureState != value.Value) { NotifyPropertyChanged("IsTransitoryFailureState"); } isTransitoryFailureState = value.Value; if (isTransitoryFailureState) IsPermanentFailureState = false; } } ```