开发者

Placeholder for a control in silverlight

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 Conte开发者_如何转开发nt="{Binding MyControl}"></ContentControl>


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜