So to do this, Microsoft has provided the class DataTemplateSelector. Using this class, one can select the data templates based on the business requirements and here is the code: The next task is to bind this newly created class named MyTemplateSelector without XAML.
So, in order to perform this task, I created a ResourceDictionary and kept both the data templates inside it as: Until here we are almost done. The only thing pending is binding our ListView control to collection and setting the cell template, that can be done as: Once everything is in place, we will get the desired output: All the preceding can be done by inheriting from the MarkupExtension class also.
I will cover that in my next article. View All. Shweta Lodha Updated date Feb 05, Next Recommended Reading. Net Core 6. Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support. Feedback will be sent to Microsoft: By pressing the submit button, your feedback will be used to improve Microsoft products and services.
Privacy policy. Thank you. Microsoft makes no warranties, express or implied, with respect to the information provided here. Gets or sets the type for which this DataTemplate is intended. This property is very similar to the TargetType property of the Style class. When you set this property to the data type without specifying an x:Key , the DataTemplate gets applied automatically to data objects of that type. Note that when you do that the x:Key is set implicitly.
We can set HorizontalContentAlignment to Stretch on the ListBox to make sure the width of the items takes up the entire space:. The current presentation does not tell us whether a Task is a home task or an office task. Our application now looks like the following.
Home tasks appear with a yellow border and office tasks appear with an aqua border:. In this example the DataTrigger uses a Setter to set a property value. The trigger classes also have the EnterActions and ExitActions properties that allow you to start a set of actions such as animations.
In addition, there is also a MultiDataTrigger class that allows you to apply changes based on multiple data-bound property values. An alternative way to achieve the same effect is to bind the BorderBrush property to the TaskType property and use a value converter to return the color based on the TaskType value.
Creating the above effect using a converter is slightly more efficient in terms of performance. Additionally, creating your own converter gives you more flexibility because you are supplying your own logic. Ultimately, which technique you choose depends on your scenario and your preference.
For information about how to write a converter, see IValueConverter. In the previous example, we placed the trigger within the DataTemplate using the DataTemplate. Triggers property. The Setter of the trigger sets the value of a property of an element the Border element that is within the DataTemplate.
However, if the properties that your Setters are concerned with are not properties of elements that are within the current DataTemplate , it may be more suitable to set the properties using a Style that is for the ListBoxItem class if the control you are binding is a ListBox.
For example, if you want your Trigger to animate the Opacity value of the item when a mouse points to an item, you define triggers within a ListBoxItem style. For an example, see the Introduction to Styling and Templating Sample. In general, keep in mind that the DataTemplate is being applied to each of the generated ListBoxItem for more information about how and where it is actually applied, see the ItemTemplate page.
Your DataTemplate is concerned with only the presentation and appearance of the data objects. In most cases, all other aspects of presentation, such as what an item looks like when it is selected or how the ListBox lays out the items, do not belong in the definition of a DataTemplate. For an example, see the Styling and Templating an ItemsControl section. In The DataType Property section, we discussed that you can define different data templates for different data objects.
That is especially useful when you have a CompositeCollection of different types or collections with items of different types. In the Use DataTriggers to Apply Property Values section, we have shown that if you have a collection of the same type of data objects you can create a DataTemplate and then use triggers to apply changes based on the property values of each data object. However, triggers allow you to apply property values or start animations but they don't give you the flexibility to reconstruct the structure of your data objects.
Some scenarios may require you to create a different DataTemplate for data objects that are of the same type but have different properties. For example, when a Task object has a Priority value of 1 , you may want to give it a completely different look to serve as an alert for yourself.
In that case, you create a DataTemplate for the display of the high-priority Task objects. Let's add the following DataTemplate to the resources section:. This example uses the DataTemplate. Resources property. Resources defined in that section are shared by the elements within the DataTemplate.
To supply logic to choose which DataTemplate to use based on the Priority value of the data object, create a subclass of DataTemplateSelector and override the SelectTemplate method. In the following example, the SelectTemplate method provides logic to return the appropriate template based on the value of the Priority property.
0コメント