Change Data Template dynamically

Source: Internet
Author: User

1. Attached Property bound to task State. Any change will dynamically set data template.
2. Visual State Manager

 

ADataTemplateSelectorDoes not respondPropertyChangeEvents, so it doesn' t get re-evaluated when your properties change.

The alternative I use isDataTriggersThat changesTemplateBased on a property.

For example, this will draw allTaskModelObjects usingContentControl, AndContentControl.TemplateIs based onTaskStatusProperty ofTaskModel

<DataTemplate x:Key="OpenTaskTemplate" TargetType="{x:Type local:TaskModel}">     <TextBlock Text="I‘m an Open Task" /></DataTemplate> <DataTemplate x:Key="ClosedTaskTemplate" TargetType="{x:Type local:TaskModel}">     <TextBlock Text="I‘m a Closed Task" /> </DataTemplate><DataTemplate DataType="{x:Type local:TaskModel}">     <ContentControl Content="{Binding }">         <ContentControl.Style>             <Style TargetType="{x:Type ContentControl}">                 <!-- Default Template -->                 <Setter Property="ContentTemplate" Value="{StaticResource OpenTaskTemplate}" />                 <!-- Triggers to change Template -->                 <Style.Triggers>                     <DataTrigger Binding="{Binding TaskStatus}" Value="Closed">                         <Setter Property="ContentTemplate" Value="{StaticResource ClosedTaskTemplate}" />                     </DataTrigger>                 </Style.Triggers>             </Style>         </ContentControl.Style>     </ContentControl> </DataTemplate>

From: https://stackoverflow.com/questions/13136816/change-data-template-dynamically

Change Data Template dynamically

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.