wpf - 为什么我的双向 WPF 绑定(bind)变得分离?

标签 wpf data-binding

在初始值设置为 true 后,我与切换按钮的双向绑定(bind)被分离。当我取消按钮时,它不再被绑定(bind)。

我有两个切换按钮:

<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>

我希望绑定(bind)到的 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 )并隐藏另一个。我需要做的是告诉我的底层 View 模型 View 是隐藏的,这样我就可以停止一个刷新一些数据的计时器。在加载时设置 IsChecked 值后,绑定(bind)由于某种原因被分离。这是运行跟踪后的输出:
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

最佳答案

你确定它真的没有“绑定(bind)”吗?我也遇到过这种情况,看起来好像在你的身上不见了......你没有任何迹象表明

NotifyOnSourceUpdated=true

在您的 {binding Mode.... } 内容中

关于wpf - 为什么我的双向 WPF 绑定(bind)变得分离?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14887045/

相关文章:

c# - MVVM WPF 主从组合框

c# - 如何在列表框 wpf 中获取多个选定项?

wpf - 如何在 WPF 中创建偏离中心的 PerspectiveCamera?

c# - 在 ListBoxItem DataTemplate - WPF 内的嵌套控件中处理相同的单击事件

data-binding - 从 Controller 中的参数绑定(bind) Grails 日期

android - 未解析的引用 : databinding in Android studio 4. 1.3

silverlight - ItemsControl 中的数据绑定(bind)到自定义 UserControl 属性

c# - 绑定(bind)Command时绑定(bind)IsEnabled是可选的吗?

c# - WPF:访问同一控件中的两个 DataContext

c# - 确定哪个文本框触发了文本更改事件