WPF ToggleButton IsChecked绑定问题 [英] WPF ToggleButton IsChecked binding issue

查看:2046
本文介绍了WPF ToggleButton IsChecked绑定问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个或两个 ToggleButton 的分组可以随时开启的情况。我的问题是如果我更改了支持变量的状态,UI状态不会更新。

I am trying to create a situation where one or none of a grouping of two ToggleButtons 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.

我有 INotifyPropertyChanged 实现

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

I've created my ToggleButtons 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>

这是我的后备属性(我使用MVVM模式,其他绑定工作,IE点击 ToggleButton 输入这些属性设置当我通过代码更改状态时,切换按钮不会改变视觉状态IE我将backing属性设置为false ,但按钮保持检查。

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


推荐答案

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

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.

这篇关于WPF ToggleButton IsChecked绑定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆