在初始值设置为true后,我对切换按钮的双向绑定将被分离。当我解开按钮时,它不再受约束。
我有两个切换按钮:
<RadioButton x:Name="BackupButton" Style="{StaticResource {x:Type ToggleButton}}" DataContext="{Binding BackupVM}" IsChecked="{Binding Mode=TwoWay, Path=IsViewVisible}">Backup</RadioButton>
<RadioButton x:Name="RestoreButton" Style="{StaticResource {x:Type ToggleButton}}">Restore</RadioButton>
我希望绑定到的BackupViewModel(实例化为BackupVM)中的属性:
private bool _IsViewVisible = true;
public bool IsViewVisible
{
get { return _IsViewVisible; }
set
{
if (value != _IsViewVisible)
{
_IsViewVisible = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("IsViewVisible"));
}
}
}
当切换一个时,我显示一个特定的用户控件(视图)并隐藏另一个。我需要做的是告诉我的基础视图模型隐藏视图,以便我可以停止刷新某些数据的计时器。在加载时设置IsChecked值后,由于某种原因绑定将被分离。以下是运行跟踪后的输出:
System.Windows.Data Warning: 52 : Created BindingExpression (hash=9343812) for Binding (hash=58368655)
System.Windows.Data Warning: 54 : Path: 'IsViewVisible'
System.Windows.Data Warning: 57 : BindingExpression (hash=9343812): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 58 : BindingExpression (hash=9343812): Attach to System.Windows.Controls.RadioButton.IsChecked (hash=17818390)
System.Windows.Data Warning: 63 : BindingExpression (hash=9343812): Resolving source
System.Windows.Data Warning: 66 : BindingExpression (hash=9343812): Found data context element: RadioButton (hash=17818390) (OK)
System.Windows.Data Warning: 74 : BindingExpression (hash=9343812): Activate with root item <null>
System.Windows.Data Warning: 102 : BindingExpression (hash=9343812): Item at level 0 is null - no accessor
System.Windows.Data Warning: 76 : BindingExpression (hash=9343812): TransferValue - got raw value {DependencyProperty.UnsetValue}
System.Windows.Data Warning: 84 : BindingExpression (hash=9343812): TransferValue - using fallback/default value 'False'
System.Windows.Data Warning: 85 : BindingExpression (hash=9343812): TransferValue - using final value 'False'
System.Windows.Data Warning: 92 : BindingExpression (hash=9343812): Got PropertyChanged event from RadioButton (hash=17818390) for DataContext
System.Windows.Data Warning: 75 : BindingExpression (hash=9343812): Deactivate
System.Windows.Data Warning: 99 : BindingExpression (hash=9343812): Replace item at level 0 with {NullDataItem}
System.Windows.Data Warning: 74 : BindingExpression (hash=9343812): Activate with root item BackupViewModel (hash=58266349)
System.Windows.Data Warning: 104 : BindingExpression (hash=9343812): At level 0 - for BackupViewModel.IsViewVisible found accessor RuntimePropertyInfo(IsViewVisible)
System.Windows.Data Warning: 100 : BindingExpression (hash=9343812): Replace item at level 0 with BackupViewModel (hash=58266349), using accessor RuntimePropertyInfo(IsViewVisible)
System.Windows.Data Warning: 97 : BindingExpression (hash=9343812): GetValue at level 0 from BackupViewModel (hash=58266349) using RuntimePropertyInfo(IsViewVisible): 'True'
System.Windows.Data Warning: 76 : BindingExpression (hash=9343812): TransferValue - got raw value 'True'
System.Windows.Data Warning: 85 : BindingExpression (hash=9343812): TransferValue - using final value 'True'
System.Windows.Data Warning: 75 : BindingExpression (hash=9343812): Deactivate
System.Windows.Data Warning: 99 : BindingExpression (hash=9343812): Replace item at level 0 with {NullDataItem}
System.Windows.Data Warning: 59 : BindingExpression (hash=9343812): Detach
答案 0 :(得分:1)
你确定它真的没有“束缚”吗?我已经把它发生在我身上,看起来像你的遗失...你没有任何迹象表明
NotifyOnSourceUpdated =真
在你的{binding Mode ....}内容
中答案 1 :(得分:0)
它似乎是一种语法的东西......它似乎根本没有绑定到你的ViewModel
。试试这个:
<RadioButton Grid.Row="0" x:Name="BackupButton" Style="{StaticResource {x:Type ToggleButton}}" IsChecked="{Binding IsViewVisible, Mode=TwoWay}">
Backup
<RadioButton.DataContext>
<local:BackupVM />
</RadioButton.DataContext>
</RadioButton>
您将local namespace
定义为:
xmlns:local="clr-namespace:WpfApplication1"
注意:将WpfApplication1
替换为命名空间的名称。