Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional Data Templates in WPF

How would it be possible to choose a different datatemplate accroding the a field in the data binding source? I would have 2 different DataTemplates

<DataTemplate x:Key="DataTemplateLabel">
    <Label Width="60" Height="25" Background="Red">
    <TextBlock Text="{Binding Path=Name}"/>
    </Label>
</DataTemplate>



<DataTemplate x:Key="DataTemplateTxtBox">
    <TextBox Width="60" Height="25" Background="Red" Text="{Binding Path=Text}"/>
</DataTemplate>

if (isText) Then use DataTemplateTxtBox ELSE use DataTemplateLabel) Is this possible? Thanks.

like image 271
Eli Perpinyal Avatar asked Oct 21 '09 12:10

Eli Perpinyal


1 Answers

You can create a DataTemplateSelector and assign it to the ContentTemplateSelector property (or ItemTemplateSelector if it is in an ItemsControl)

like image 169
Thomas Levesque Avatar answered Oct 03 '22 18:10

Thomas Levesque