Data template selector




















In this example, the binding source is a list of Task objects. One of the properties of a Task object is Priority. There are two data templates defined, myTaskTemplate and importantTaskTemplate.

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. The template to return is found in the resources of the enveloping Window element.

To use the template selector resource, assign it to the ItemTemplateSelector property of the ListBox. The call passes the data object as the item parameter. This enables multiple DataTemplate instances to be applied to the same type of object, to customize the appearance of particular objects.

Skip to main content. This browser is no longer supported. Download Microsoft Edge More info. Contents Exit focus mode. Creating a Xamarin. Please rate your experience Yes No. Any additional feedback?

Privacy policy. Thank you. Microsoft makes no warranties, express or implied, with respect to the information provided here. Provides a way to choose a DataTemplate based on the data object and the data-bound element. In this example, the binding source is a list of Task objects.

One of the properties of a Task object is Priority. There are two data templates defined, myTaskTemplate and importantTaskTemplate. 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.

The template to return is found in the resources of the enveloping Window element. OK, now it is time to throw all of the xaml out there so that you can see how the ListView actually uses the ImgStringTemplateSelector we just created:. The templates and the selector are all resources in the current window.

This makes it so that when we get down to creating the ListView , we can set the ItemTemplateSelector to the static resource imgStringTemplateSelector. What is the element This? Well, it happens to be the name of the window — you can see it up in the Name property of the Window tag. And the PathCollection is a property on Window1 :. Add "Sunset. Add "Blue hills. Add "Water lilies. Add "Winter. The only other thing of note is the fact that ScrollViewer.

CanContentScroll is set to false on the ListView. It is just a nicety because the ListView will contain images. Well, that is it for this introduction to the DataTemplateSelector. You can grab the Visual Studio project for what we wrote today here.



0コメント

  • 1000 / 1000