如何防止使用全局DataTemplate而不指定新的DataTemplate

时间:2013-01-19 19:07:22

标签: wpf datatemplate itemscontrol itemtemplate

<DataTemplate DataType="{x:Type MyType}">
    ...
</DataTemplate>

我有MyType的默认DataTemplate。 想要防止在下面使用它而不必指定真正的DataTemplate

<ItemsControl ItemsSource="{whateverList of MyType}" ItemTemplate="{x:Null}"/>

ItemTemplate="{x:Null}"无法完成工作 - &gt;显示默认的DataTemplate 会对“ToString()”显示

感到满意

任何想法?

1 个答案:

答案 0 :(得分:0)

如果要覆盖该类型的默认数据模板,则必须指定另一个:

<ItemsControl ItemsSource="{listOfMyType}">
  <ItemsControl.ItemTemplate>
    <DataTemplate>
      <!-- Whatever -->
    </DataTemplate>
  </ItemsControl.ItemTemplate>
</ItemsControl>

有关此方法的更多讨论here,包括其他人尝试将{x:Null}用于模板。

如果您的模板需要绑定到您的类型的ToString()(并且类型中没有属性可以为您执行此操作),则需要使用IValueConverter,如上所述{ {3}}