首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WPF ToggleButton IsChecked绑定问题

WPF ToggleButton IsChecked绑定问题
EN

Stack Overflow用户
提问于 2009-09-04 12:20:25
回答 2查看 11.4K关注 0票数 3

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

我确实实现了INotifyPropertyChanged

我像这样创建了我的ToggleButton

代码语言:javascript
复制
        <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确实会进入这些属性设置。当我通过代码更改状态时,切换按钮不会更改视觉状态。例如,我将backing属性设置为false,但按钮保持选中状态。

代码语言:javascript
复制
    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;
        }
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2009-09-04 12:31:41

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

代码语言:javascript
复制
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;
    }
}

顺便说一句,你说当你使用接口而不是代码时,它是有效的,但我看不出它是可能的。

票数 8
EN

Stack Overflow用户

发布于 2009-09-04 12:31:59

您的代码看起来是错误的:您在进行更改之前就通知了它。我认为你需要把你的isPermFailure = value.Value;移到里面:

代码语言:javascript
复制
        if (isPermFailure != value.Value)            
        {
            isPermFailure = value.Value;
            NotifyPropertyChanged("IsPermanentFailureState");
        }

对于另一个也是如此。

我想你也会想要把另一条语句移进去:

代码语言:javascript
复制
        if (isPermFailure != value.Value)            
        {
            isPermFailure = value.Value;
            NotifyPropertyChanged("IsPermanentFailureState");
            if (isPermFailure) 
                IsTransitoryFailureState = false;
        }

否则,您将不必要地设置状态并通知该状态。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1378894

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档