有没有一种方法可以仅使用C#构建DataTemplate [英] Is there a way to build a DataTemplate with only C#
问题描述
是否有一种无需使用已弃用的数据模板即可构建的 FrameworkElementFactory
或 XamlReader.Load
字符串解释方法,对代码(相关问题)或(另一个相关)?
Is there a way to build a DataTemplate without using the deprecated FrameworkElementFactory
or the XamlReader.Load
method of string interpretation to code (related question) or (another related)?
DataTemplate dt = new DataTemplate();
StackPanel sp = new StackPanel();
ComboBox cellComboBox = new ComboBox() { Visibility = Visibility.Collapsed };
CheckBox cellCheckBox = new CheckBox() { Visibility = Visibility.Collapsed };
sp.Children.Add(cellComboBox);
sp.Children.Add(cellCheckBox);
// then add the stackpanel somehow?
更新:为什么?
这是一个新程序可能需要多年的支持。 XamlReader字符串在运行时进行解析,以获取具有构造函数,方法,属性...的东西。 FrameworkElementFactory
已弃用了多个版本,但从未真正替换过。
Update: Why?
This is for a new program that may have to be supported for years. XamlReader string parsing at runtime for something that's got a constructor, methods, properties... feels kludgy. The FrameworkElementFactory
has been deprecated for several versions but has never had a real replacement.
推荐答案
有没有一种方法可以只使用C#构建数据模板
Is there a way to build a DataTemplate with only C#
是,您可以使用 FrameworkElementFactory
或 XamlReader.Load
方法创建 DataTemplate
Yes, you use either the FrameworkElementFactory
or the XamlReader.Load
method to create a DataTemplate
programmatically.
...,而无需使用已弃用的
FrameworkElementFactory
或XamlReader.Load
方法?
简短回答:否
为什么已经有两种方法又需要另一种方法?另外请注意,如果您不想使用字符串,则最好使用 FrameworkElementFactory
类。尽管MSDN上的文档说了什么,但在.NET Framework 4.7.2中并未真正将这种类型标记为过时或不推荐使用。
Why would you need yet another way when there are two already? Also note that it's perfectly fine to use the FrameworkElementFactory
class if you don't want to use strings. Despite what the documentation on MSDN says, this type is not really marked as obsolete or deprecated in .NET Framework 4.7.2.
这篇关于有没有一种方法可以仅使用C#构建DataTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!