Yanko Pata
Yanko Pata

Reputation: 175

Static resource DataTemplate vs. inline DataTemplate

  1. Lets say I got a CellContentTemplate (of a column in a DataGrid), and I define its DataTemplate inline:

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

Is there a difference in terms of memory usage in comparison to referencing by StaticResource to pre-defined DataTemplate? After all the content of the DataTemplateis copied either way.

  1. 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?

Upvotes: 1

Views: 355

Answers (1)

mm8
mm8

Reputation: 169210

The DataTemplate itself will only be created once regardless of whether you create it as a reuasable resource or inline. So there is no difference in terms of memory usage unless you create another instance of the DataGrid. Then there will be another instance of the inline DataTemplate created.

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?

The string "Green" that you defined in your XAML markup is resolved to Brushes.Green which is a static property of the Brushes class that returns the same cached and frozen brush each time.

Upvotes: 3

Related Questions