將初始值設置爲true後,我對雙向切換按鈕的雙向綁定會分離。當我解開按鈕時,它不再被綁定。爲什麼我的雙向WPF綁定變得分離?
我有兩個開關按鈕:
<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"));
}
}
}
當一個被切換的I顯示特定用戶控制(查看)並隱藏其他。我需要做的是告訴我的底層視圖模型,該視圖是隱藏的,所以我可以停止刷新一些數據的計時器。在加載時設置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
我試過了,它也不起作用。我知道它是負載的,因爲我可以驗證調試器中調用getter。但是當我點擊另一個按鈕時,setter從不會被調用。 – JRadness 2013-02-15 17:21:30
@JRadness,是的,你是正確的,它將在調試時第一次通過「Getter」綁定。但是,您需要告訴控件...除了第一次啓動時,我希望此綁定的另一側告訴我它何時更改,以便我可以再次更新。這就是「NotifyOnSourceUpdated = true」的意思。不要讓VS2010在我面前,但是還有一些其他部分需要與您需要的綁定類似。 – DRapp 2013-02-15 22:08:38