Purpose of using FrameworkElementFactory
Asked Answered
R

1

7

In one of the application I am working I have found this code -

public class MatrixCellTemplate : ColumnDataTemplate<MatrixCellContainer>
{
}

public class ColumnDataTemplate<T> : DataTemplate where T : FrameworkElement
{
    public ColumnDataTemplate()
    {
        FrameworkElementFactory factory = new FrameworkElementFactory(typeof(T));
        VisualTree = factory;
    }
}

This MatrixCellTemplate is used to set the CellTemplate of a custom DataGridTemplateColumn (later added to DataGrid.Columns collection) like this -

<DataGridTemplateColumn.CellTemplate>
    <Matrix:MatrixCellTemplate />
</DataGridTemplateColumn.CellTemplate>

I am not sure what is the benefit of using this FrameworkElementFactory and what problem I can face if I directly use MatrixCellContainer as cell template -

<DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <Matrix:MatrixCellContainer>
        </Matrix:MatrixCellContainer>
    </DataTemplate>
    <!--<Matrix:MatrixCellTemplate />-->
</DataGridTemplateColumn.CellTemplate>
Retral answered 1/2, 2012 at 6:41 Comment(1)
This looks to me like a bit of overengineering. The original author looks like they were trying to guarantee that a MatrixCellContainer was always used as the root of the DataGridCell. FrameworkElementFactory is the only way to make a template through code (unless you want to parse XAML at runtime - yuck). I see no functional difference with what you propose though.Curassow
P
9

Hard to guess the reasoning behind this code, maybe to reduce the XAML, but you really might as well define it manually, functionally it does not matter (especially since there is nothing dynamic in there that would justify doing it in code-behind in the first place).

In fact FrameworkElementFactory is deprecated anyway:

This class is a deprecated way to programmatically create templates, which are subclasses of FrameworkTemplate such as ControlTemplate or DataTemplate; not all of the template functionality is available when you create a template using this class. The recommended way to programmatically create a template is to load XAML from a string or a memory stream using the Load method of the XamlReader class.

Pulpwood answered 1/2, 2012 at 6:55 Comment(6)
Thanks H.B., +1 for the deprecated info. I will still be looking to know in which cases it makes sense to use FrameworkElementFactory with DataGrid.Retral
One of purposes is performance. If you have a lot of controls with complex templates, the creation of controls from XAML is much slower than creation from code.Cnidoblast
@TcKs: Show me your empirical data as i won't believe it otherwise, we are talking about templates here, the controls are not instantiated until needed anyway. If you define a template in code or in XAML should not matter at all, both are compiled unless you use loose XAML which is unlikely.Pulpwood
Its in complex project. However, if you use WrapPanel (has no virtualizing alternative), the 500 items are created from the xaml dom. The speed of creating the items was +- 15 sec. After rewriting the xaml to C# equivalent, it's under one sec.Cnidoblast
@TcKs: Interesting, though i would try to avoid such scenarios to begin with.Pulpwood
@Cnidoblast I would personally cache the template rather than parsing/loading the XAML every time.Ultramicroscope

© 2022 - 2024 — McMap. All rights reserved.