silverlight 中控件的占位符

发布于 2024-09-28 23:39:46 字数 191 浏览 0 评论 0原文

silverlight 中是否有类似占位符的控件? 如果我的视图模型中有一个动态创建的控件,如何将其绑定到 Xaml 中的某个元素? 这是实现这一目标的正确方法吗:

<ContentControl Content="{Binding MyControl}"></ContentControl>

Is there something like a placeholder for a control in silverlight?
If I have a dynamically created control in my view-model how can I bind it to some element in Xaml?
Is it the right way to achieve that:

<ContentControl Content="{Binding MyControl}"></ContentControl>

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

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

发布评论

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

评论(2

何以心动 2024-10-05 23:39:46

不要动态创建实际控件。将您需要的所有内容放入视图模型的属性中,并将 UI 绑定到这些属性。当然,您还需要将控件的 DataContext 设置为视图模型。有关详细信息,请参阅此概述

要回答更多问题,请将 ContentControl 的内容绑定到 ViewModel 中的属性。该属性可以是任何类型!然后在 XAML 中声明一个 DataTemplate 用于显示该属性;这将代表绑定后用于装饰该属性的“皮肤”。将 DataTemplate 设置为 ContentControl 的 ContentTemplate(或该类型的默认值...等)。 DataTemplate 可以包含其他控件、其他绑定或任何内容。这至少是一种的实现方法,有很多种。您必须询问更具体的问题或开始阅读基础知识。

Don't create actual controls dynamically. Put everything you need in properties of the view-model and bind the UI to those properties. You will also need the control's DataContext to be set to the view-model, of course. See this overview for details.

To answer more to the point, bind the content of the ContentControl to a property in the ViewModel. That property can be of any type! Then in XAML declare a DataTemplate to be used for displaying that property; that will represent the 'skin' used to dress up that property after it's bound. Set the DataTemplate as the ContentTemplate of the ContentControl (or as the default for that type... etc.). The DataTemplate can contain other controls, other bindings, anything. That's at least one way of doing it, there are many. You'll have to ask something more specific or start reading on the basics.

·深蓝 2024-10-05 23:39:46

您可以有一个占位符,根据您尝试显示的对象类型,用适当的子控件填充该占位符。例如,您可以在视图模型中绑定到 Foo 类型的属性,然后视图将自动选择 FooView 控件来显示该属性。请参阅此其他问题以及示例代码有关其工作原理的链接文章 - 它涉及数据模板。确保你首先理解这些东西。

来自您的其他问题我知道您实际上正在尝试使用 MEF 添加视图。本质上,您正在尝试使用 MEF 插件添加数据模板。这是一个更高级的用例,也是此问题的主题。该问题的答案解释了如何动态合并插件提供的资源字典(其中包含数据模板信息)。

You can have a placeholder which is filled in with the appropriate child control based on the type of object you are trying to show. For example, you could bind to a property of type Foo in your viewmodel, and then the view would automatically select a FooView control for showing that property. See this other question and the sample code in the linked article about how that works - it involves Data Templates. Make sure you understand that stuff first.

From your other question I know that you are actually trying to add views with MEF. In essence, you are trying to add data templates with MEF plugins. That's a more advanced use case which is the topic of this question. The answer to that question explains how you can dynamically merge the resource dictionaries (which contain the data template information) provided by plugins.

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