将大量数据传递到用户控件中

发布于 10-09 16:08 字数 577 浏览 9 评论 0原文

由于 WPF 中的 UserControl 必须具有无参数构造函数,因此为它们提供“接近”构造时所需的相当复杂的数据的正确方法是什么。我已尝试为此使用依赖属性,但遇到了 Visual Studio 设计器在尝试将 Dictionary 等内容传递到 IDictionary时遇到的问题; 类型化依赖属性。在某些时候,它必须需要精确的编译时类型匹配,否则 XAML 不会出现在设计器中,尽管应用程序执行得很好。

基本上,我想要一种好的方法来将通常传递到构造函数中的内容传递到用户控件中。最好的办法是什么?

更新: 有问题的用户控件将始终从 XAML 创建,因此除了无参数构造之外还不能选择非无参数构造。

更新2: 一个有趣的想法是从无参数构造函数中获取一些可访问的东西,我可以从中获取初始化数据。也许会问这样的问题:我已经初始化的祖先中的哪一个实现了 IMyDataProvider 接口?这可能类似于相对源到祖先类型绑定的工作方式,只不过是通过用户控件构造函数以编程方式完成的。

Since UserControls in WPF have to have parameterless constructors, what is the correct way for supplying them with fairly complex data that is needed "near" the time of construction. I have tried using dependency properties for this, but am running into issues with the Visual Studio designer balking at attempts to pass stuff like a Dictionary<string,MyObject> into an IDictionary<string,MyObject> typed dependency property. At some point it must want an exact compile time type match, or the XAML doesn't come up in the designer, although the application executes just fine.

Basically, I want a good way to pass in stuff that I would normally pass into a constructor into a User Control. What's the best way?

Update:
The user control in question will always be created from XAML, so having a non-parameterless construction in addition to the parameterless one is not an option.

Update 2:
An interesting idea would be to have something accessible from the parameterless constructor that I can get my initialization data from. Something like perhaps asking the question: Which of my already initialized ancestors implements an IMyDataProvider interface? This could be similar to how the relative source to ancestor type bindings work, except done programatically from the user control constructor.

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

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

发布评论

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

评论(2

泛泛之交2024-10-16 16:08:28

如果您遇到的唯一问题是传入派生类型,则可以传入一个简单的具体容器类,其中包含复杂类型作为属性。例如:

public class InitializationData
{
    public IDictionary<TKey, TValue> Dictionary { get; set; }
}

这种间接级别将克服 Visual Studio 设计器的限制。

If the only problem you are having is passing in derived types, you can pass in instead a simple concrete container class containing your complex types as properties. For example:

public class InitializationData
{
    public IDictionary<TKey, TValue> Dictionary { get; set; }
}

This level of indirection will overcome the limitations of the Visual Studio designer.

骷髅2024-10-16 16:08:28

有几个选择。

1,您可以有多个构造函数,一个无参数构造函数用于通过 XAML 创建控件时,另一个构造函数需要一组参数用于直接通过代码创建控件。如果您绝对不想通过代码创建实例,那么...

2、添加一个公共属性,该属性只有一个 setter,并使用您想要传入并用作初始化控件的数据的确切字典类型进行定义。该属性只需要调用一次。您可以拥有其他属性,即 getters/setters,它们公开已初始化的数据以便更通用的类型。

A couple of options.

1, You can have more than one constructor, a parameterless one for when your control is created via XAML and another that takes a set of parameters for when you create it directly via code. If you definitely don't want to create your instance via code then...

2, Add a public property that only has a setter and is defined with the exact dictionary type you want to pass in and use as the data for initializing the control. The property only needs to be called once. You can have other properties that are getters/setters that expose that initialized data in order more generic types.

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