Sowen
Sowen

Reputation:

Is it possible to make ItemsControl smart enough to change DataTemplate according to different data?

This might be a crazy question.

Suppose a collection that has a a few entries, some of them are just empty string. Is it possible to write the ItemsControl to use different DateTempalte if the content is empty string?

Thanks a million times

Upvotes: 0

Views: 405

Answers (2)

Anvaka
Anvaka

Reputation: 15823

Yeah. There are many ways to do this. Most common: (DataTemplate + DataTrigger) or ItemTemplateSelector.

In DataTemplate+DataTrigger scenario in the DataTrigger you will update content of your DataTemplate via setters. With ItemTemplateSelector you can decide based on your item which DataTemplate to use.

I really encourage you to read wonderful series of articles by dr. Wpf "ItemsControl: A to Z". If you spend 4 - 6 hours on reading today you will save much more hours later, when you find yourself one on one with problems in ItemsControl (hopefully not right before the deadline).

More details on your particular question is here: 'D' is for DataTemplate

Upvotes: 4

TalkingCode
TalkingCode

Reputation: 13557

Maybe this will help

Different DataTemplates for different data

http://www.thejoyofcode.com/Different_DataTemplates_for_different_data.aspx

it was really helpful for me.

Upvotes: 1

Related Questions