【问题标题】:C#/WPF: Dependency Property is not updating the bound Property?C#/WPF:依赖属性没有更新绑定的属性?
【发布时间】:2010-12-26 10:59:51
【问题描述】:

我正在尝试将一个依赖属性从我的 UserControl 绑定到我的 MainViewModel。

这是 DependencyProperty 的样子:

    public static DependencyProperty ItemHasChangesProperty = DependencyProperty.Register("ItemHasChanges",
                                                                                  typeof(bool),
                                                                                  typeof(MyUserControl),
                                                                                  new PropertyMetadata(null));
    public bool ItemHasChanges
    {
        get { return (bool)GetValue(ItemHasChangesProperty); }
        set { SetValue(ItemHasChangesProperty, value); }
    }

我的 XAML:

  <local:MyUserControl ItemHasChanges="{Binding Path=Changes}" Grid.Row="4"   />

现在在调试和检查 bool Changes 的 Set-Accessor 时,我发现当我在 UserControl ItemHasChanges = true; 中设置时它永远不会被访问

知道我在这里做错了什么吗?

谢谢!

干杯

【问题讨论】:

    标签: c# wpf dependency-properties dependencies


    【解决方案1】:

    明白了..我不得不改变

    <local:MyUserControl ItemHasChanges="{Binding Path=Changes}" Grid.Row="4"   />
    

    到

    <local:MyUserControl ItemHasChanges="{Binding Path=Changes, Mode=OneWayToSource}" Grid.Row="4"   />
    

    我花了大约 3 小时才弄明白.. 哈哈 :-)

    干杯

    【讨论】:

    • 供阅读本文的其他人参考,在通用 Windows 应用程序中,没有 OneWayToSource 模式。相反,我使用了OneWay。
    • 如果您需要双向更新,TwoWay 也可以使用。这也浪费了我大约一个小时,以为我的财产没有设定它的价值。
    【解决方案2】:

    您是否直接在控件上设置ItemHasChanges(如,而不是通过更新绑定源)?如果是这样,那将删除绑定。

    【讨论】:

    • 不,我将源绑定到控件,并根据操作更改源的值..
    猜你喜欢
    • 2012-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-31
    • 2023-03-26
    • 2011-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多