在设计GUI时,我在xaml文件中为形状定义了一个样式,如下所示:
<Style x:Key="Data" TargetType="Rectangle" BasedOn="{StaticResource FlowChartRectangleStyle}">
</Style>
<Style x:Key="Data_DragThumb" TargetType="Rectangle" BasedOn="{StaticResource Data}">
<Setter Property="IsHitTestVisible" Value="true"/>
<Setter Property="Height" Value="300"/>
<Setter Property="Width" Value="200"/>
</Style>
<Rectangle Style="{StaticResource Data}" ToolTip="Data" StrokeThickness="2">
<s:DesignerItem.DragThumbTemplate>
<ControlTemplate>
<Rectangle Style="{StaticResource Data_DragThumb}"/>
</ControlTemplate>
</s:DesignerItem.DragThumbTemplate>
</Rectangle>
在保存输出时,输出xml文件中存在此形状的content属性。现在我想使用相同的形状(更准确地说,它的内容)来创建具有相同内容属性的XElement。为此我尝试了这个:
XElement myNewItem;
... ... //assign some other property values here
Object dataShape = Application.Current.Resources["Data_DragThumb"];
myNewItem.Content = dataShape;
但是这并没有给出GUI输出xml文件中出现的形状的确切内容值,而只是给出类似&#34; system.windows.style&#34;的字符串。实际上,我希望在可视化“我的新项目”时看到相同的形状。在给出内容值之后。我怎么解决这个问题?谢谢。
更新
输出xml文件将形状存储为如下数据:
<Content><Rectangle StrokeThickness="2" ToolTip="Data" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:dd="clr-namespace:DiagramDesigner;assembly=ADAX_GUI"><Rectangle.Style><Style TargetType="Rectangle"><Style.BasedOn><Style TargetType="Rectangle"><Style.Resources><ResourceDictionary /></Style.Resources><Setter Property="Shape.Fill"><Setter.Value><LinearGradientBrush StartPoint="0,0" EndPoint="0,1"><LinearGradientBrush.GradientStops><GradientStop Color="#FFFAFBE9" Offset="0" /><GradientStop Color="#FFFFA500" Offset="1" /></LinearGradientBrush.GradientStops></LinearGradientBrush></Setter.Value></Setter><Setter Property="Shape.Stroke"><Setter.Value><SolidColorBrush>#FFD69436</SolidColorBrush></Setter.Value></Setter><Setter Property="Shape.StrokeThickness"><Setter.Value><s:Double>1</s:Double></Setter.Value></Setter><Setter Property="Shape.StrokeLineJoin"><Setter.Value><x:Static Member="PenLineJoin.Round" /></Setter.Value></Setter><Setter Property="Shape.Stretch"><Setter.Value><x:Static Member="Stretch.Fill" /></Setter.Value></Setter><Setter Property="UIElement.IsHitTestVisible"><Setter.Value><s:Boolean>False</s:Boolean></Setter.Value></Setter><Setter Property="UIElement.SnapsToDevicePixels"><Setter.Value><s:Boolean>True</s:Boolean></Setter.Value></Setter></Style></Style.BasedOn><Style.Resources><ResourceDictionary /></Style.Resources></Style></Rectangle.Style><dd:DesignerItem.DragThumbTemplate><ControlTemplate><Rectangle Name="DataShape" Tag="DataShapeTag"><Rectangle.Style><Style TargetType="Rectangle"><Style.BasedOn><Style TargetType="Rectangle"><Style.BasedOn><Style TargetType="Rectangle"><Style.Resources><ResourceDictionary /></Style.Resources><Setter Property="Shape.Fill"><Setter.Value><LinearGradientBrush StartPoint="0,0" EndPoint="0,1"><LinearGradientBrush.GradientStops><GradientStop Color="#FFFAFBE9" Offset="0" /><GradientStop Color="#FFFFA500" Offset="1" /></LinearGradientBrush.GradientStops></LinearGradientBrush></Setter.Value></Setter><Setter Property="Shape.Stroke"><Setter.Value><SolidColorBrush>#FFD69436</SolidColorBrush></Setter.Value></Setter><Setter Property="Shape.StrokeThickness"><Setter.Value><s:Double>1</s:Double></Setter.Value></Setter><Setter Property="Shape.StrokeLineJoin"><Setter.Value><x:Static Member="PenLineJoin.Round" /></Setter.Value></Setter><Setter Property="Shape.Stretch"><Setter.Value><x:Static Member="Stretch.Fill" /></Setter.Value></Setter><Setter Property="UIElement.IsHitTestVisible"><Setter.Value><s:Boolean>False</s:Boolean></Setter.Value></Setter><Setter Property="UIElement.SnapsToDevicePixels"><Setter.Value><s:Boolean>True</s:Boolean></Setter.Value></Setter></Style></Style.BasedOn><Style.Resources><ResourceDictionary /></Style.Resources></Style></Style.BasedOn><Style.Resources><ResourceDictionary /></Style.Resources><Setter Property="UIElement.IsHitTestVisible"><Setter.Value><s:Boolean>True</s:Boolean></Setter.Value></Setter><Setter Property="FrameworkElement.Height"><Setter.Value><s:Double>300</s:Double></Setter.Value></Setter><Setter Property="FrameworkElement.Width"><Setter.Value><s:Double>200</s:Double></Setter.Value></Setter><Setter Property="FrameworkElement.Tag"><Setter.Value>DataShape</Setter.Value></Setter></Style></Rectangle.Style></Rectangle></ControlTemplate></dd:DesignerItem.DragThumbTemplate></Rectangle> </Content>
我尝试获取相同的内容值并将其设置为:
myNewItem.Content = (the above shown content)
但这不适合我。我想要做的是,在我的代码中,我通过使用某种方法手动设置内容,这样代码生成的结构与我在GUI中绘制的形状完全相同。