WPF中的条件数据模板

Eli*_*yal 6 wpf binding datatemplate

如何选择一个不同的数据模板来匹配数据绑定源中的字段?我会有2个不同的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>
Run Code Online (Sandbox Code Playgroud)

if(isText)然后使用DataTemplateTxtBox ELSE使用DataTemplateLabel)这可能吗?谢谢.

Tho*_*que 7

您可以创建一个DataTemplateSelector并将其分配给ContentTemplateSelector属性(或者ItemTemplateSelector如果它在一个属性中ItemsControl)