2013-02-15 36 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