The only possibility that makes sense is to include some controls in the DataTemplate and use this. DataTemplate "dresses" any object and can include full control; in fact, it is quite common to create custom controls and build them using DataTemplates; for example, you can define a DataTemplate as follows:
<DataTemplate TargetType="{x:Type viewmodels:MyViewModel}"> <Button Command={Binding MyCommand} Content={Binding MyCaption} /> </DataTemplate>
Assuming that the MyViewModel parameter contains the string property MyCaption and the ICommand property called MyCommand, the DataTemplate will automatically be used, if defined within the scope, to visually display an instance of the MyViewModel class if:
- an instance is set as the Content property of a ContentControl or derived control, or
- An instance is one of many items in a collection that is displayed using the ItemsControl element.
When I say βin scope,β what I really mean is a DataTemplate defined in a ResourceDictionary somewhere in an accessible hierarchy of controls; i.e. Application.Resources, Window.Resources, etc., up to the Grid.Resources level, if the target control (ContentControl or ItemsControl) is placed in it.
Of course, you can also specify a DataTemplate without a TargetType, but use x: Key instead, and use it manually, referring to that key; for example myListBox.ItemTemplate={StaticResource myKey} .
source share