2013-02-15 45 views
2

將初始值設置爲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 

回答

1

您確定它確實沒有「綁定」嗎?我有它發生在我身上,看上去就像它在你的失蹤......你有什麼指示

NotifyOnSourceUpdated =真

在{結合模式....}內容

+0

我試過了,它也不起作用。我知道它是負載的,因爲我可以驗證調試器中調用getter。但是當我點擊另一個按鈕時,setter從不會被調用。 – JRadness 2013-02-15 17:21:30

+0

@JRadness,是的,你是正確的,它將在調試時第一次通過「Getter」綁定。但是,您需要告訴控件...除了第一次啓動時,我希望此綁定的另一側告訴我它何時更改,以便我可以再次更新。這就是「NotifyOnSourceUpdated = true」的意思。不要讓VS2010在我面前,但是還有一些其他部分需要與您需要的綁定類似。 – DRapp 2013-02-15 22:08:38

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

+0

我綁定到我的viewmodel。該窗口有一個數據上下文,其中包含我的BackupViewModel名稱BackupVM的實例。所以我添加了DataContext =「{Binding BackupVM}」。 – JRadness 2013-02-15 17:39:27

+0

@JRadness:你的單選按鈕的父控件是否也有其'DataContext'集? – Mash 2013-02-15 18:18:09

+0

@JRadness或代碼隱藏中的任何內容在運行時更改單選按鈕或任何父母的'DataContext'?如果不是,那麼嘗試從'RadioButton'中刪除'DataContext' ...它應該從父'DataContext'繼承。 – Mash 2013-02-15 18:20:39