由于WPF中的GroupBox控件只接受一个WPF控件作为内容,我必须首先将所有属性包装到DockPanel控件中。
我使用以下xsl样式表将我的代码转换为之后显示的XAML代码段 样式表(摘录):
<!-- Default attribute processing -->
<xsl:template name="process-element">
<xsl:param name="attr" />
<!-- Process all attributes and elements which are going to be
transformed to attributes -->
<xsl:apply-templates select="@*|*" mode="to-attr" />
<!-- Add extra attribute -->
<xsl:if test="$attr">
<xsl:attribute name="{substring-after($attr, '|')}">
<xsl:value-of select="@*[local-name() = substring-before($attr, '|')]" />
</xsl:attribute>
</xsl:if>
<!-- Process children elements -->
<xsl:apply-templates select="*" />
</xsl:template>
<!-- Map GroupBoxWrapper into GroupBox -->
<xsl:template match="GroupBoxWrapper">
<xsl:element name="GroupBox">
<!-- TODO: Add DockPanel Element and move "cursor" one level upwards -->
<!--<xsl:element name="DockPanel">-->
<xsl:call-template name="process-element">
<xsl:with-param name="attr"/>
</xsl:call-template>
<!--</xsl:element>-->
</xsl:element>
</xsl:template>
这是生成的代码XAML:
<GroupBox Name="groupbox1" DockPanel.Dock="Left, Right, Top, Bottom" Width="1092" Height="125" Background="Transparent" Foreground="#0046D5" Visibility="visible" BorderThickness="1,1,1,1" FontFamily="Tahoma" FontSize="9" TabIndex="0" IsTabStop="False">
<DockPanel Name="panel4" DockPanel.Dock="Top" Width="1078" Height="25" Background="Transparent" Visibility="visible">
<Label Name="lblName" DockPanel.Dock="Left" Width="100" Height="25" Background="Transparent" Foreground="Black" Visibility="visible" BorderThickness="1,1,1,1" FontFamily="Tahoma" FontSize="9" TabIndex="0" IsTabStop="False" Content="Name:" />
<TextBox Name="txtName" DockPanel.Dock="Left" Width="100" Height="25" Background="Azure" Foreground="Black" Visibility="visible" BorderThickness="1,1,1,1" FontFamily="Tahoma" FontSize="9" TabIndex="1" IsTabStop="True" Text="" />
</DockPanel>
<DockPanel Name="panel5" DockPanel.Dock="Top" Width="1078" Height="25" Background="Transparent" Visibility="visible">
<Label Name="lblLastName" DockPanel.Dock="Left" Width="100" Height="25" Background="Transparent" Foreground="Black" Visibility="visible" BorderThickness="1,1,1,1" FontFamily="Tahoma" FontSize="9" TabIndex="0" IsTabStop="False" Content="Lastname:" />
<TextBox Name="txtLastName" DockPanel.Dock="Left" Width="100" Height="25" Background="Azure" Foreground="Black" Visibility="visible" BorderThickness="1,1,1,1" FontFamily="Tahoma" FontSize="9" TabIndex="1" IsTabStop="True" Text="" />
</DockPanel>
<DockPanel Name="panel6" DockPanel.Dock="Top" Width="1078" Height="25" Background="Transparent" Visibility="visible">
<Label Name="label4" DockPanel.Dock="Left" Width="100" Height="25" Background="Transparent" Foreground="Black" Visibility="visible" BorderThickness="1,1,1,1" FontFamily="Tahoma" FontSize="9" TabIndex="0" IsTabStop="False" Content="Age:" />
<TextBox Name="textbox3" DockPanel.Dock="Left" Width="100" Height="25" Background="Azure" Foreground="Black" Visibility="visible" BorderThickness="1,1,1,1" FontFamily="Tahoma" FontSize="9" TabIndex="1" IsTabStop="True" Text="" />
</DockPanel>
<DockPanel Name="panel14" DockPanel.Dock="Top" Width="1078" Height="25" Background="Transparent" Visibility="visible">
<Label Name="label9" DockPanel.Dock="Left" Width="100" Height="25" Background="Transparent" Foreground="Black" Visibility="visible" BorderThickness="1,1,1,1" FontFamily="Tahoma" FontSize="9" TabIndex="0" IsTabStop="False" Content="Gender:" />
<RadioButton Name="radiobutton1" DockPanel.Dock="Left" Width="75" Height="25" Background="Transparent" Foreground="Black" Visibility="visible" BorderThickness="1,1,1,1" FontFamily="Tahoma" FontSize="9" TabIndex="1" IsTabStop="True" Content="Male" />
<RadioButton Name="radiobutton2" DockPanel.Dock="Left" Width="75" Height="25" Background="Transparent" Foreground="Black" Visibility="visible" BorderThickness="1,1,1,1" FontFamily="Tahoma" FontSize="9" TabIndex="2" IsTabStop="True" Content="Female" />
</DockPanel>
</GroupBox>
问题是GroupBox现在包含四个在WPF中无法实现的元素。这就是我需要将这些控件包装到单个DockPanel中的原因。
当我取消注释<xsl:element name="DockPanel">
行时,XAML代码如下所示:
<GroupBox> <!-- attributes should appear on this line -->
<DockPanel Name="groupbox1" DockPanel.Dock="Left, Right, Top, Bottom" Width="1092" Height="125" Background="Transparent" Foreground="#0046D5" Visibility="visible" BorderThickness="1,1,1,1" FontFamily="Tahoma" FontSize="9" TabIndex="0" IsTabStop="False">
<DockPanel Name="panel4" DockPanel.Dock="Top" Width="1078" Height="25" Background="Transparent" Visibility="visible">
<Label Name="lblName" DockPanel.Dock="Left" Width="100" Height="25" Background="Transparent" Foreground="Black" Visibility="visible" BorderThickness="1,1,1,1" FontFamily="Tahoma" FontSize="9" TabIndex="0" IsTabStop="False" Content="Name:" />
<TextBox Name="txtName" DockPanel.Dock="Left" Width="100" Height="25" Background="Azure" Foreground="Black" Visibility="visible" BorderThickness="1,1,1,1" FontFamily="Tahoma" FontSize="9" TabIndex="1" IsTabStop="True" Text="" />
</DockPanel>
<DockPanel Name="panel5" DockPanel.Dock="Top" Width="1078" Height="25" Background="Transparent" Visibility="visible">
<Label Name="lblLastName" DockPanel.Dock="Left" Width="100" Height="25" Background="Transparent" Foreground="Black" Visibility="visible" BorderThickness="1,1,1,1" FontFamily="Tahoma" FontSize="9" TabIndex="0" IsTabStop="False" Content="Lastname:" />
<TextBox Name="txtLastName" DockPanel.Dock="Left" Width="100" Height="25" Background="Azure" Foreground="Black" Visibility="visible" BorderThickness="1,1,1,1" FontFamily="Tahoma" FontSize="9" TabIndex="1" IsTabStop="True" Text="" />
</DockPanel>
<DockPanel Name="panel6" DockPanel.Dock="Top" Width="1078" Height="25" Background="Transparent" Visibility="visible">
<Label Name="label4" DockPanel.Dock="Left" Width="100" Height="25" Background="Transparent" Foreground="Black" Visibility="visible" BorderThickness="1,1,1,1" FontFamily="Tahoma" FontSize="9" TabIndex="0" IsTabStop="False" Content="Age:" />
<TextBox Name="textbox3" DockPanel.Dock="Left" Width="100" Height="25" Background="Azure" Foreground="Black" Visibility="visible" BorderThickness="1,1,1,1" FontFamily="Tahoma" FontSize="9" TabIndex="1" IsTabStop="True" Text="" />
</DockPanel>
<DockPanel Name="panel14" DockPanel.Dock="Top" Width="1078" Height="25" Background="Transparent" Visibility="visible">
<Label Name="label9" DockPanel.Dock="Left" Width="100" Height="25" Background="Transparent" Foreground="Black" Visibility="visible" BorderThickness="1,1,1,1" FontFamily="Tahoma" FontSize="9" TabIndex="0" IsTabStop="False" Content="Gender:" />
<RadioButton Name="radiobutton1" DockPanel.Dock="Left" Width="75" Height="25" Background="Transparent" Foreground="Black" Visibility="visible" BorderThickness="1,1,1,1" FontFamily="Tahoma" FontSize="9" TabIndex="1" IsTabStop="True" Content="Male" />
<RadioButton Name="radiobutton2" DockPanel.Dock="Left" Width="75" Height="25" Background="Transparent" Foreground="Black" Visibility="visible" BorderThickness="1,1,1,1" FontFamily="Tahoma" FontSize="9" TabIndex="2" IsTabStop="True" Content="Female" />
</DockPanel>
</DockPanel>
</GroupBox>
所有属性,其中GroupBox的意图位于嵌套内部GroupBox元素的DockPanel元素中。
我需要弄清楚如何创建DockPanel元素(检查)并向上移动到GroupBox元素以调用process-element模板。
有什么想法吗?
答案 0 :(得分:0)
<强>问题强>
流程元素模板旨在执行以下操作:
您目前遇到的问题是因为您要在步骤1中附加属性(&lt; xsl:apply-templates select =“@ | ”mode =“to-attr”&gt;)到新创建的元素DockPanel(&lt; xsl:element name =“DockPanel”&gt;)。这就是您的属性被复制到其他元素中的原因。
<强>解强>
不要依赖模板process-elements来进行这种转换。最初,当某个点(XML to XAML transformation using XSLT 1.0 | Exclude specific transformations for some controls)的所有元素都遵循不同的逻辑来保存重复代码时,就会编写该模板。因此,如果您要实现的目标不遵循先前的模式,则必须修改流程模板或为GroupBoxWrapper元素编写不同的模板。
以下模板将修复您要实现的目标。
<!-- Map GroupBoxWrapper into GroupBox -->
<xsl:template match="GroupBoxWrapper">
<xsl:element name="GroupBox">
<!-- Process attributes -->
<xsl:apply-templates select="@*|*" mode="to-attr" />
<!-- Create new DockPanel to wrap children -->
<xsl:element name="DockPanel">
<!-- Process GroupBoxWrapper chldren -->
<xsl:apply-templates select="*" />
</xsl:element>
</xsl:element>
</xsl:template>
PS。我认为你应该首先理解我们[stackoverflow上的人们]发布的代码,然后再实际使用它,这样你就不必每次出现问题时都依赖我们。