简体   繁体   English

如何在WPF中创建网格并使用FrameworkElementFactory设置RowDefinitations?

[英]How to create a Grid and set RowDefinitations with FrameworkElementFactory in WPF?

I want to use FrameworkElementFactory to create a grid template, but how to define its RowDefinition s ? 我想使用FrameworkElementFactory创建网格模板,但是如何定义其RowDefinition

as the code 作为代码

FrameworkElementFactory gridFactory = new FrameworkElementFactory(typeof(Grid));
FrameworkElementFactory RowDefinitionFactory1 = new FrameworkElementFactory(typeof(RowDefinition));
gridFactory.AppendChild(RowDefinitionFactory1);
FrameworkElementFactory RowDefinitionFactory2 = new FrameworkElementFactory(typeof(RowDefinition));
gridFactory.AppendChild(RowDefinitionFactory2);

it will throw exception... 它将引发异常...

because the <RowDefinitation> should be under <Grid.RowDefinitations> 因为<RowDefinitation>应该在<Grid.RowDefinitations>

FrameworkElementFactory has been deprecated: 不推荐使用FrameworkElementFactory

This class is a deprecated way to programmatically create templates, which are subclasses of FrameworkTemplate such as ControlTemplate or DataTemplate; 此类是通过编程方式创建模板的不推荐使用的方法,这些模板是FrameworkTemplate的子类,例如ControlTemplate或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. 以编程方式创建模板的推荐方法是使用XamlReader类的Load方法从字符串或内存流中加载XAML。

The suggested route is to use XamlReader.Load or XamlReader.Parse if you need to programmatically create templates. 如果需要以编程方式创建模板,建议的方法是使用XamlReader.LoadXamlReader.Parse

var template = 
    "<Grid>" +
        "<Grid.RowDefinitions>" +
            "<RowDefinition/>" +
            "<RowDefinition/>" +
        "</Grid.RowDefinitions>" +
    "</Grid>";

var markup = XamlReader.Parse(template);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM