c# - 有没有办法只用 C# 构建 DataTemplate

标签 c# wpf datatemplate

有没有一种方法可以不使用 deprecated 来构建数据模板? FrameworkElementFactoryXamlReader.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#

是的,您可以使用 FrameworkElementFactoryXamlReader.Load 方法以编程方式创建 DataTemplate

...without using the deprecated FrameworkElementFactory or the XamlReader.Load method?

简短的回答:没有。

既然已经有两种方法,为什么还需要另一种方法呢?另请注意,如果您不想使用字符串,则使用 FrameworkElementFactory 类完全没问题。不管 MSDN 上的文档怎么说,这种类型在 .NET Framework 4.7.2 中并没有真正标记为过时或弃用。

关于c# - 有没有办法只用 C# 构建 DataTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50571658/

相关文章:

允许函数评估的 JavaScript 模板

wpf - 如何让 DataTemplate.DataTrigger 检查大于或小于?

wpf - 我是否可以在MVVM设计模式下从ViewModel或Model调用服务?

c# - 在 WPF 中实现自定义\用户控件的最佳实践

c# - 删除特殊字符并排除一个词

c# - wkHTMLtoPDF 内存损坏和 AccessViolationException

wpf - 需要良好的WPF MVVM教程

wpf - 为什么要添加 x :Name attribute to a user control cause a compilation error?

c# - 在应用程序域之间来回传递集合对象

c# - 如何获取 System.Xml.Linq.XNode 的名称?