使用 FrameworkElementFactory 的目的 [英] Purpose of using FrameworkElementFactory

查看:40
本文介绍了使用 FrameworkElementFactory 的目的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在使用的其中一个应用程序中,我找到了此代码 -

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;
    }
}

这个MatrixCellTemplate用于设置自定义DataGridTemplateColumnCellTemplate(后来添加到DataGrid.Columns集合)像这样 -

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>

我不确定使用这个 FrameworkElementFactory 有什么好处,以及如果我直接使用 MatrixCellContainer 作为单元格模板会面临什么问题 -

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>

推荐答案

很难猜出这段代码背后的原因,也许是为了减少 XAML,但你真的还不如手动定义它,功能上没有关系(特别是因为那里没有任何动态可以证明首先在代码隐藏中进行).

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).

实际上 FrameworkElementFactory 无论如何都已弃用:

In fact FrameworkElementFactory is deprecated anyway:

该类是一种不推荐使用的以编程方式创建模板的方式,模板是 FrameworkTemplate 的子类,例如 ControlTemplate 或 DataTemplate;使用此类创建模板时,并非所有模板功能都可用.以编程方式创建模板的推荐方法是使用 XamlReader 类的 Load 方法从字符串或内存流加载 XAML.

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.

这篇关于使用 FrameworkElementFactory 的目的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆