由于 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 元素以调用流程元素模板。
有任何想法吗?