为什么我的双向WPF绑定会变得分离?

4

当我将初始值设置为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"));
            }
        }
    }

当一个被切换时,我会显示一个特定的用户控件 (view) 并隐藏另一个。我需要做的是告诉我的底层视图模型该视图已经被隐藏了,以便我可以停止刷新一些数据的计时器。在 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
2个回答

1

你确定它真的没有“绑定”吗?我也遇到过这种情况,看起来在你的代码中确实缺失了一些内容……你的代码中没有任何指示

NotifyOnSourceUpdated=true

在你的{binding Mode....}内容中。


我尝试了这个,但它也不起作用。我知道它绑定在加载时,因为我可以在调试器中验证getter被调用。但是当我点击另一个按钮时,setter从未被调用。 - JRadness
@JRadness,是的,你说得对,当调试时,它将通过“Getter”第一次绑定。但是,你需要告诉控件...除了第一次启动之外,我希望此绑定的另一侧在更改时通知我,以便我可以再次获取更新。这就是“NotifyOnSourceUpdated=true”的作用...我没有VS2010在面前,但还有其他几个类似的绑定部分是需要的,你可能也需要。 - DRapp

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>

在编程中,当你定义本地命名空间时:

xmlns:local="clr-namespace:WpfApplication1"

注意:请将WpfApplication1替换为您的命名空间名称。

我正在绑定到我的视图模型。这个窗口有一个数据上下文,其中包含我的BackupViewModel实例名BackupVM。因此,我添加了DataContext =“{Binding BackupVM}”。 - JRadness
@JRadness:你的单选按钮的任何父控件是否也设置了它们的“DataContext”? - Mash
@JRadness 或者你的代码后台是否有任何更改单选按钮或任何父级运行时的 DataContext 的操作?如果没有,请尝试从你的 RadioButton 中移除 DataContext... 它应该会从父级 DataContext 继承。 - Mash
即使我将要绑定的属性移动到主数据上下文,我仍然得到相同的行为。 - JRadness
@JRadness 我就是直接拿你的代码,把第一个单选按钮中的 DataContext="{Binding BackupVM}" 文本删除了,它就开始工作了。一旦我把它放回去,它就停止工作了。我也将我的窗口 DataContext 设置为 BackupVM - Mash

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接