绑定到自定义控件属性

发布于 2024-12-07 18:13:34 字数 1486 浏览 1 评论 0原文

我正在开发一个帐户列表下拉控件,该控件将在我的应用程序中使用。下拉列表将调用服务并检索可用帐户的列表,并将公开下拉列表中所选项目的属性“SelectedAccount”。 SelectedAccount 将是一个 DependencyProperty,因为它必须由 AccountDropdown 控件的使用者绑定,并且它必须是双向可绑定的,以便它反映现有的 SelectedAccount。

AccountDropdown.asmx 很简单,它包含一个 ComboBox:

<ComboBox SelectedItem="{Binding Path=SelectedAccount, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"...

组合框的选定项绑定到一个注册为

DependencyProperty.Register("SelectedAccount", typeof(IAccount), typeof(AccountDropdown),
                                new UIPropertyMetadata(null));

... 的属性,以及常用属性:

    #region SelectedAccount

    /// <summary>
    /// Selected account
    /// </summary>
    public IAccount SelectedAccount
    {
        get { return (IAccount)GetValue(SelectedAccountProperty); }
        set { SetValue(SelectedAccountProperty, value); }
    }
    #endregion SelectedAccount

这些属性在代码隐藏文件中定义...并且数据上下文设置为“this”...因此绑定设置正确。

当我使用此控件时,我需要将 SelectedAccount 绑定到另一个视图的 ViewModel 的属性,例如:

<Controls:AccountDropdown SelectedAccount="{Binding Path=SelectedAccount, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />

当我运行上面的代码并更改下拉列表中的选择时,我收到 System.StackOverflow 异常。我什至无法调试它,异常是从 Windows.Base.dll 引发的。

我已经在这个问题上挣扎了一整天了......任何帮助将不胜感激。

**注意:**我已经编写了几个具有依赖属性的 WPF 控件,它们工作正常,但当我使用它们时,我在 .asmx 文件中显式提供值。在这种情况下,当我使用该属性时,我会再次绑定。我猜测公开可绑定属性可能需要一些额外的技巧。

I am developing an Account List dropdown control which is to be used across my application. The dropdown will make a call to a service and retrieve the list of available accounts, and will expose a property "SelectedAccount" for the selected item in the dropdown. The SelectedAccount is going to be a DependencyProperty as it has to be bound by the consumers of the AccountDropdown control, and it has to be two-way bindable so it reflects an existing SelectedAccount.

The AccountDropdown.asmx is simple, it contains a ComboBox:

<ComboBox SelectedItem="{Binding Path=SelectedAccount, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"...

The selected item of the combo box is bound to a property which is registered as a

DependencyProperty.Register("SelectedAccount", typeof(IAccount), typeof(AccountDropdown),
                                new UIPropertyMetadata(null));

... and the usual property:

    #region SelectedAccount

    /// <summary>
    /// Selected account
    /// </summary>
    public IAccount SelectedAccount
    {
        get { return (IAccount)GetValue(SelectedAccountProperty); }
        set { SetValue(SelectedAccountProperty, value); }
    }
    #endregion SelectedAccount

The properties are defined in the code-behind file... and the data context is set to "this"... so the binding is set correctly.

When I use this control, I need to bind the SelectedAccount to the property of a ViewModel for another view, e.g.:

<Controls:AccountDropdown SelectedAccount="{Binding Path=SelectedAccount, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />

When I run the above code, and I change the selection on the dropdown, I get a System.StackOverflow exception. I can't even debug it, the exception is thrown from Windows.Base.dll.

I've been struggling with this one for a day now... any help would be appreciated.

**Note: **I have written several WPF Controls with Dependency Properties, and they work fine, but when I use them, I'm providing the value explicitly in the .asmx file. In this case I'm binding again when I use the property. I am guessing that exposing a bindable property may require some additional trick.

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

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

发布评论

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

评论(2

月亮是我掰弯的 2024-12-14 18:13:34

好吧,就像通常的情况一样,解决方案很简单。 AccountDropdown 控件的 DataContext 被隐式设置为其自身,因此出现无限循环。只需指定 DataContext 以使用正在使用它的控件的 DataContext(因此使用预期的 ViewModel 绑定):

<Controls:AccountDropdown DataContext="{Binding}" SelectedAccount="{Binding Path=SelectedAccount, Mode=TwoWay}" />

我仍然需要弄清楚如何告诉控件使用容纳它的控件的数据上下文。 ..如果我确实弄清楚了,我会将其发布在这里...或者如果你们中有人知道,请透露一些信息。

再次感谢那些回复的人。

Ok, as is often the case, the solution was simple. The DataContext of the AccountDropdown control was being implicitly set to itself, hence the infinite loop. Simply specifying the DataContext to use the DataContext of the control where it was being used (and therefore using the intended ViewModel binding):

<Controls:AccountDropdown DataContext="{Binding}" SelectedAccount="{Binding Path=SelectedAccount, Mode=TwoWay}" />

I still have to figure out how to tell the control to use the data context of the control that is housing it... if I do figure it out, I will post it here... or if any of you know, please shed some light.

Thanks again to those that responded.

一抹苦笑 2024-12-14 18:13:34

我猜想 SelectedAccountChanged 方法会更新该字段,从而导致无限递归,从而导致堆栈溢出。

要测试:从 DependencyProperty.Register 调用中删除 SelectedAccountChanged 以确保这是错误的来源,然后检查方法本身。

如果您需要帮助,请在那里发布方法。

I guess the SelectedAccountChanged method updates the field and thereby causes infinite recursion which results in a stack overflow.

To test: remove the SelectedAccountChanged from the DependencyProperty.Register call to make sure that this is the sources of the error and then examine the method itself.

If you need help post the method there.

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