有没有一种方法可以不使用 deprecated 来构建数据模板? FrameworkElementFactory
或 XamlReader.Load
方法将字符串解释为代码 (related question)或 (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
已被多个版本弃用,但从未有真正的替代品。
最佳答案
Is there a way to build a DataTemplate with only C#
是的,您可以使用 FrameworkElementFactory
或 XamlReader.Load
方法以编程方式创建 DataTemplate
。
...without using the deprecated
FrameworkElementFactory
or theXamlReader.Load
method?
简短的回答:没有。
既然已经有两种方法,为什么还需要另一种方法呢?另请注意,如果您不想使用字符串,则使用 FrameworkElementFactory
类完全没问题。不管 MSDN 上的文档怎么说,这种类型在 .NET Framework 4.7.2 中并没有真正标记为过时或弃用。
关于c# - 有没有办法只用 C# 构建 DataTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50571658/