WPF:在属性中存储XAML并在ContentControl中显示 [英] WPF: Store XAML in Property and Display in ContentControl
问题描述
我将XML文件反序列化为一个类,然后尝试在ContentControl中显示一些XAML(存储在该类的属性中)。
I am deserializing a XML file into a class and then trying to display some XAML (stored in a property in the class) in a ContentControl.
这是我的XML:
<CallSteps>
<CallStep>
<StepID>20</StepID>
<StepName>Intro</StepName>
<StepXaml>
<![CDATA[<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:uc="clr-namespace:CallTracker.Library.UserControls.BaseUserControls;assembly=CallTracker.Library">
<uc:LabelValueControl Label="TestLabel" Value="356733" />
</StackPanel>]]>
</StepXaml>
</CallStep>
<CallStep>
<StepID>30</StepID>
<StepName>Intro</StepName>
<StepXaml>
<![CDATA[<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:uc="clr-namespace:CallTracker.Library.UserControls.BaseUserControls;assembly=CallTracker.Library">
<uc:LabelValueControl Label="TestLabel2" Value="356738124315" />
</StackPanel>]]>
</StepXaml>
</CallStep>
</CallSteps>
这正确地反序列化为 CallStep
的集合对象。这是单个 CallStep
对象的样子:
This correctly deserializes to a collection of CallStep
objects. Here is what a single CallStep
object looks like:
作为我代码的一部分,我有一个 CurrentCallStep
,其中包含单个 CallStep
。我想使用以下方式显示 ContentControl
(或其他容器)内 StepXaml
中包含的XAML:
As part of my code I have a CurrentCallStep
which contains a single CallStep
. I would like to display the XAML contained in StepXaml
within a ContentControl
(or some other container) using something like:
/// <summary>
/// Current call step object
/// </summary>
public CallStep CurrentCallStep
{
get { return _CurrentCallStep; }
set
{
_CurrentCallStep = value;
NotifyPropertyChanged(m => m.CurrentCallStep);
}
}
private CallStep _CurrentCallStep;
在视图中:
<!-- CurrentCallStep contains the XAML for the current call steps to be displayed -->
<ContentControl Content="{Binding CurrentCallStep.StepXaml}"
Background="LightBlue"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
但这不会将XAML转换为XAML,而只是显示如下文本:
This however is not converting the XAML to XAML but rather just showing the text like:
如何获取 CurrentCallStep.StepXaml
中的文本以转换为XAML?
How can I get the text in CurrentCallStep.StepXaml
to convert to XAML?
推荐答案
我能够通过从每个 StepXaml
中提取CDATA值来解决此问题,如下所示:
I was able to solve this by extracting the CDATA value from each StepXaml
as follows:
/// <summary>
/// Step XAML
/// </summary>
[XmlElement("StepXaml")]
public object StepXaml
{
get { return _StepXaml; }
set
{
if (_StepXaml != value)
{
object _obj;
using (MemoryStream stream = new MemoryStream())
{
// Convert the text into a byte array so that
// it can be loaded into the memory stream.
XmlNode _node = (value as XmlNode[])[0];
if (_node is XmlCDataSection)
{
XmlCDataSection _cDataSection = _node as XmlCDataSection;
byte[] bytes = Encoding.UTF8.GetBytes(_cDataSection.Value);
// Write the XAML bytes into a memory stream.
stream.Write(bytes, 0, bytes.Length);
// Reset the stream's current position back
// to the beginning so that when it is read
// from, the read begins at the correct place.
stream.Position = 0;
// Convert the XAML into a .NET object.
_obj = XamlReader.Load(stream);
_StepXaml = _obj;
NotifyPropertyChanged(m => m.StepXaml);
}
}
}
}
}
private object _StepXaml;
ContentControl
只是指 StepXaml
,例如:
<!-- CallContent contains the XAML for the current call steps to be displayed -->
<ContentControl Content="{Binding CurrentCallStep.StepXaml}"
这样做反序列化XML时,我不需要做任何特殊的事情。
Doing it this way I didn't have to do anything special while deserializing the XML.
这篇关于WPF:在属性中存储XAML并在ContentControl中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!