WPF ToggleButton IsChecked 绑定问题
我试图创建一种情况,其中两个 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 ToggleButton
s 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 ToggleButton
s 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题在于您在实际更改属性值之前发出属性更改通知。因此,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:
Incidentally, you say it works when you use the interface rather than code, but I can't see it possibly could.
您的代码看起来错误:您在做出更改之前就通知了更改。我认为你需要移动你的
isPermFailure = value.Value;
里面:
另一个也同样。
我认为您也想将另一条语句移到那里:
否则您将不必要地设置状态并通知该语句。
Your code looks wrong: you're notifying change before you've made it. I think you need to move your
isPermFailure = value.Value;
inside:
and likewise for the other one.
I think you'll want to move the other statement in there, too:
otherwise you'll be setting state and notifying on that one, unnecessarily.