Pie*_*eed 3 data-binding wpf togglebutton
我试图创建一种情况,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>
Run Code Online (Sandbox Code Playgroud)
这是我的支持属性(我使用MVVM模式,其他绑定工作,IE点击ToggleButton确实输入这些属性设置.当我通过代码更改状态时,切换按钮不会改变视觉状态.IE I我将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;
}
}
Run Code Online (Sandbox Code Playgroud)
问题只是您在实际更改属性值之前提出了属性更改通知.因此,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;
}
}
Run Code Online (Sandbox Code Playgroud)
顺便说一下,你说它在你使用界面而不是代码时有效,但我看不到它可能.