奇怪的 BindingMode=TwoWay 行为

发布于 2024-12-02 13:35:09 字数 898 浏览 0 评论 0原文

有这样的代码

public int SelectedPage
    {
        get { return (int)GetValue(SelectedPageeProperty); }
        set
        {
            SetValue(SelectedPageeProperty, value);
            if (NavigationCommands.Refresh.CanExecute(null, this))
                NavigationCommands.Refresh.Execute(null, this);
        }
    }

    // Using a DependencyProperty as the backing store for SelectedPage.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty SelectedPageeProperty =
        DependencyProperty.Register("SelectedPagee", typeof(int), typeof(DataBaseSettings), new UIPropertyMetadata(0));

wpf :

 <ComboBox  SelectedItem="{Binding Path=SelectedPage, Mode=TwoWay}" />

奇怪的是,如果静态依赖属性的命名类似于公共属性 SelectedPage,则绑定不起作用。但如果我稍微将 dp 重命名为 SelectedPagee,并带有 2 ee,它就可以工作。有人有可以解释这种现象的想法吗?

have this code

public int SelectedPage
    {
        get { return (int)GetValue(SelectedPageeProperty); }
        set
        {
            SetValue(SelectedPageeProperty, value);
            if (NavigationCommands.Refresh.CanExecute(null, this))
                NavigationCommands.Refresh.Execute(null, this);
        }
    }

    // Using a DependencyProperty as the backing store for SelectedPage.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty SelectedPageeProperty =
        DependencyProperty.Register("SelectedPagee", typeof(int), typeof(DataBaseSettings), new UIPropertyMetadata(0));

wpf :

 <ComboBox  SelectedItem="{Binding Path=SelectedPage, Mode=TwoWay}" />

The strange thing about this is if the static dependency property is named like the public property SelectedPage, the binding does not work. But if i slighty rename the dp to SelectedPagee with 2 ee's it works. Does anybody have an idea that could explain this phenomenon?

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

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

发布评论

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

评论(1

音盲 2024-12-09 13:35:09

如果您像这样重命名属性,则绑定将找不到它并使用 CLR 属性的 setter,否则将被完全忽略(这就是为什么您不应该在其中包含任何代码的原因)就像你所做的那样),并且你的包装器中有一些刷新逻辑(如前所述,不应该存在),它可能会刷新与绑定有关的东西,使你认为它“有效”。

如果您需要对更改的属性执行其他逻辑,请向字段的元数据注册添加 DependencyPropertyChanged 回调。

... new UIPropertyMetadata(0, SelectedPageChanged);

private static void SelectedPageChanged(object sender, DependencyPropertyChangedEventArgs e)
{
    var @this = (DataBaseSettings)sender;
    if (NavigationCommands.Refresh.CanExecute(null, @this))
        NavigationCommands.Refresh.Execute(null, @this);
}

If you rename the property like this the binding will not find it and use the setter of the CLR-property which otherwise will be completely ignored (that is the reason why you are not supposed to have any code in it like you do), and there is some refresh logic in your wrapper (which as noted should not be there) which probably does refresh something that has to do with the binding making you think that it "works".

If you need additional logic performed on property changed add a DependencyPropertyChanged callback to the meta-data registration of the field.

... new UIPropertyMetadata(0, SelectedPageChanged);

private static void SelectedPageChanged(object sender, DependencyPropertyChangedEventArgs e)
{
    var @this = (DataBaseSettings)sender;
    if (NavigationCommands.Refresh.CanExecute(null, @this))
        NavigationCommands.Refresh.Execute(null, @this);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文