【问题标题】:how to prevent using global DataTemplate without specifiying new one如何防止在不指定新的情况下使用全局 DataTemplate
【发布时间】:2013-01-19 19:07:21
【问题描述】:
<DataTemplate DataType="{x:Type MyType}">
    ...
</DataTemplate>

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

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

ItemTemplate="{x:Null}" 没有完成工作 -> 显示默认 DataTemplate 会对“ToString()”显示感到满意

有什么想法吗?

【问题讨论】:

    标签: wpf datatemplate itemscontrol itemtemplate


    【解决方案1】:

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

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

    关于这种方法here 有更多讨论,包括另一个人尝试使用{x:Null} 作为模板。

    如果您的模板需要为您的类型绑定到 ToString()(并且类型中不存在可以为您执行此操作的属性),则您需要使用 IValueConverter,如 here 所述。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 2018-10-13
      • 1970-01-01
      • 2012-08-07
      • 1970-01-01
      相关资源
      最近更新 更多