gpt4 book ai didi

c# - 从 FrameworkElement 获取 FrameworkElementFactory

转载 作者:太空宇宙 更新时间:2023-11-03 11:15:23 25 4
gpt4 key购买 nike

我有一个FrameworkElement,例如带有子元素的StackPanel,我需要创建一个DataTempltate,其VisualTree属性是我的 FrameworkElement。要完成这项工作,我需要将我的 FrameworkElement 转换为等效的 FrameworkElementFactory

我该怎么办?

最佳答案

您或许可以使用 XamlWriterXamlReader类以获取在您的控件中使用的 XAML 的副本,并使用它来构建另一个控件

string panelXaml = XamlWriter.Save(myStackPanel);

StringReader stringReader = new StringReader(panelXaml);
XmlReader xmlReader = XmlReader.Create(stringReader);
StackPanel newStackPanel = (StackPanel)XamlReader.Load(xmlReader);

我认为这只会复制 StackPanel,因此您可能也必须以相同的方式复制它的子项。

同样根据 Remarks section on MSDN for the FrameworkElementFactory

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.

关于c# - 从 FrameworkElement 获取 FrameworkElementFactory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12819670/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com