WPF ToggleButton检查绑定问题

WPF ToggleButton检查绑定问题,wpf,data-binding,togglebutton,Wpf,Data Binding,Togglebutton,我试图创建一种情况,在这种情况下,两个ToggleButtons组成的分组中的一个或任何一个都可以随时打开。我遇到的问题是,如果我更改支持变量的状态,UI状态不会更新 我确实实现了INotifyPropertyChanged 我创建了我的ToggleButtons,如下所示: <ToggleButton IsChecked="{Binding Path=IsPermanentFailureState, Mode=TwoWay}"

我试图创建一种情况,在这种情况下,两个
ToggleButton
s组成的分组中的一个或任何一个都可以随时打开。我遇到的问题是,如果我更改支持变量的状态,UI状态不会更新

我确实实现了
INotifyPropertyChanged

我创建了我的
ToggleButton
s,如下所示:

        <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>

问题只是您在实际更改属性值之前发出了属性更改通知。因此,WPF读取属性的旧值,而不是新值。更改为:

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;
    }
}

顺便说一句,您说当您使用界面而不是代码时,它可以工作,但我看不出它可能工作。

您的代码看起来是错误的:您在进行更改之前通知了更改。我想你需要移动你的手臂 isPermFailure=value.value; 内部:

另一个也一样

我想你也应该把另一句话放进去:

        if (isPermFailure != value.Value)            
        {
            isPermFailure = value.Value;
            NotifyPropertyChanged("IsPermanentFailureState");
            if (isPermFailure) 
                IsTransitoryFailureState = false;
        }

否则,您将设置状态并在不必要的情况下发出通知。

您比我早了一步,肯特。从我这里得到一支向上的箭。哇——我不该这么蠢!这可能就是当你一直坐着独自编写代码时发生的事情。谢谢关于你最后的评论:不,它不会不必要地通知你。它所做的只是不必要地调用该属性。setter逻辑只有在基础字段发生更改时才会导致通知。您完全正确:不小心选择了单词,但我仍然认为我的答案更好。:-)
        if (isPermFailure != value.Value)            
        {
            isPermFailure = value.Value;
            NotifyPropertyChanged("IsPermanentFailureState");
        }
        if (isPermFailure != value.Value)            
        {
            isPermFailure = value.Value;
            NotifyPropertyChanged("IsPermanentFailureState");
            if (isPermFailure) 
                IsTransitoryFailureState = false;
        }