WPF中的条件数据模板
如何根据数据绑定源中的a字段选择不同的datatemplate? 我会有两个不同的数据模板WPF中的条件数据模板,wpf,binding,datatemplate,Wpf,Binding,Datatemplate,如何根据数据绑定源中的a字段选择不同的datatemplate? 我会有两个不同的数据模板 <DataTemplate x:Key="DataTemplateLabel"> <Label Width="60" Height="25" Background="Red"> <TextBlock Text="{Binding Path=Name}"/> </Label> </DataTemplate> <D
<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>
如果是(isText),则使用DataTemplateTxtBox,否则使用DataTemplateLabel)这可能吗?
谢谢。这更像是一个间接的回答,但通过执行多态性(即放弃IsText属性并使用不同的类型回答问题),您可以为每个不同的类型定义一个DataTemplate。您可以创建一个
DataTemplateSelector
并将其分配给ContentTemplateSelector
属性(或ItemTemplateSelector
如果它位于ItemsControl
中)