将大量数据传递到用户控件中
由于 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 技术交流群。
发布评论
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如果您遇到的唯一问题是传入派生类型,则可以传入一个简单的具体容器类,其中包含复杂类型作为属性。例如:
这种间接级别将克服 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:
This level of indirection will overcome the limitations of the Visual Studio designer.