3

いつでも 2 つのグループの 1 つまたは 1 つもToggleButtonオンにできない状況を作成しようとしています。私が抱えている問題は、バッキング変数の状態を変更すると、UI の状態が更新されないことです。

私はINotifyPropertyChanged実装しました。

私はToggleButton次のように自分のを作成しました:

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

これは私のバッキング プロパティです (私は MVVM パターンを使用しています。他のバインディングは機能します。IE をクリックすると、ToggleButtonこれらのプロパティ設定に入ります。コードで状態を変更している場合、トグル ボタンは視覚的な状態を変更しません。IE Iバッキング プロパティを false に設定していますが、ボタンはチェックされたままです。

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

2 に答える 2

8

問題は、実際にプロパティ値を変更する前にプロパティ変更通知を発生させていることです。したがって、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;
    }
}

ちなみに、コードではなくインターフェイスを使用すると機能するとおっしゃっていますが、実際に機能するとは思えません。

于 2009-09-04T12:31:40.710 に答える
0

コードが間違っているように見えます。変更を行う前に変更を通知しています。isPermFailure = value.Value; を移動する必要があると思います。中身:

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

もう一方についても同様です。

他のステートメントもそこに移動したいと思うでしょう:

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

そうしないと、不必要に状態を設定して通知することになります。

于 2009-09-04T12:31:58.773 に答える