我正在尝试创建一种情况,在这种情况下,两个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确实会进入这些属性设置。当我通过代码更改状态时,切换按钮不会更改视觉状态。例如,我将backing属性设置为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;
}
}发布于 2009-09-04 12:31:41
问题是您在实际更改属性值之前引发了属性更改通知。因此,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-04 12:31:59
您的代码看起来是错误的:您在进行更改之前就通知了它。我认为你需要把你的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;
}否则,您将不必要地设置状态并通知该状态。
https://stackoverflow.com/questions/1378894
复制相似问题