C#/ WPF:依赖属性是不是更新绑定属性?

时间:2009-12-14 14:50:59

标签: c# wpf dependency-properties dependencies

我正在尝试将来自UserControl的Dependency属性绑定到我的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;

中设置时,它永远不会被访问

知道我在这里做错了吗?

谢谢!

干杯

2 个答案:

答案 0 :(得分:9)

知道了......我不得不改变

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

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

花了我3个小时来搞清楚..哈哈: - )

干杯

答案 1 :(得分:0)

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

相关问题