0

由于 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 元素以调用流程元素模板。

有任何想法吗?

4

1 回答 1

0

问题

流程元素模板旨在执行以下操作:

  1. 将当前属性和当前元素的子元素转换为新元素的属性。
  2. 使用 attr 参数添加额外的属性
  3. 正常处理孩子。

您当前出现的问题是因为您将步骤 1 (<xsl:apply-templates select="@ | " mode="to-attr">) 中的属性附加到新创建的元素 DockPanel (<xsl:element name ="DockPanel">)。这就是为什么您的属性被复制到其他元素中的原因。

解决方案

不要依赖模板流程元素进行此转换。最初,该模板是在特定点的所有元素(使用 XSLT 1.0 的 XML 到 XAML 转换 | 排除某些控件的特定转换)遵循不同的逻辑来保存重复代码时编写的。因此,如果您要实现的目标不遵循以前的模式,则必须修改流程模板或为 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 上的人员] 发布的代码,然后再实际使用它,这样您就不必每次出现问题时都依赖我们。

于 2013-03-01T15:12:47.770 回答