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

发布于 2024-08-14 21:25:46 字数 1001 浏览 2 评论 0原文

我正在尝试将依赖属性从我的 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 中设置时,它永远不会被访问;

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

谢谢!

干杯

I'm trying to bind a Dependency Property from my UserControl to my MainViewModel.

This is how the DependencyProperty looks like:

    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); }
    }

My XAML:

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

Now when debugging and checking the Set-Accessor of bool Changes, I see that it never gets accessed when I set in the UserControl ItemHasChanges = true;

Any idea what I'm doing wrong here?

Thanks!

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟花易冷人易散 2024-08-21 21:25:46

明白了..我不得不改为

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

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

了我大约3小时才弄清楚..哈哈:-)

干杯

Got it.. I had to change

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

to

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

Took me about 3h to figure it out.. haha :-)

Cheers

一生独一 2024-08-21 21:25:46

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

Are you setting ItemHasChanges on the control directly (as in, not by updating the binding source)? If so, that will remove the binding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文