WPF ToggleButton IsChecked 绑定问题

发布于 2024-08-03 19:31:49 字数 1904 浏览 3 评论 0原文

我试图创建一种情况,其中两个 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 的视觉状态,我将支持属性设置为 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;
        }
    }

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.

I do have INotifyPropertyChanged implemented.

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>

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

执笏见 2024-08-10 19:31:49

问题在于您在实际更改属性值之前发出属性更改通知。因此,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;
    }
}

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

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.

相权↑美人 2024-08-10 19:31:49

您的代码看起来错误:您在做出更改之前就通知了更改。我认为你需要移动你的
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;
        }

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

Your code looks wrong: you're notifying change before you've made it. I think you need to move your
isPermFailure = value.Value;
inside:

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

and likewise for the other one.

I think you'll want to move the other statement in there, too:

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

otherwise you'll be setting state and notifying on that one, unnecessarily.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文