静态资源 DataTemplate 与内联 DataTemplate

Static resource DataTemplate vs. inline DataTemplate

  1. 假设我有一个 CellContentTemplate(属于 DataGrid 中的一列),我定义了它的 DataTemplate 内联:

    <Column.CellContentTemplate>
        <DataTemplate>
    
        </DataTemplate> 
    </Column.CellContentTemplate>
    

与通过 StaticResource 引用预定义的 DataTemplate 相比,在内存使用方面是否存在差异? 毕竟 DataTemplate 的所有内容都被复制了。

  1. 假设此 DataTemplate 中有 TextBlockForeground 中的 "Green"。如果我通过 StaticResource 引用 DataTemplate 还是被复制,这个 "Green" 笔刷是否会逐行重复使用?

无论您是将其创建为可重用资源还是内联资源,DataTemplate 本身都只会创建一次。因此,除非您创建 DataGrid 的另一个实例,否则在内存使用方面没有区别。然后将创建另一个内联实例 DataTemplate

Let's say this DataTemplate has TextBlock with Foreground of "Green" in it. Does this "Green" brush is reused from row to row if I reference to DataTemplate by StaticResource or is it copied?

您在 XAML 标记中定义的字符串 "Green" 解析为 Brushes.Green,它是 Brushes [=26] 的静态 属性 =] returns 每次都使用相同的缓存和冻结画笔。