由於WPF中的GroupBox控件只接受一個WPF控件作爲內容,所以我必須首先將所有屬性包裝到DockPanel控件中。XSL轉換| XML到XAML |添加層次結構級別以嵌套元素
我用下面的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>
全部屬性,其意圖在那裏對分組框中是其中嵌套的內分組框中元件DockPanel中元件。
我需要弄清楚如何創建DockPanel元素(檢查)並向上移動到GroupBox元素來調用過程元素模板。
任何想法?
你的輸入XML是什麼樣的?你想要的輸出是什麼樣的?模式爲'to-attr'的模板是什麼樣的?您不能首先創建DockPanel元素,然後移出它以添加屬性,因此您需要一種替代方法,這需要您提供更多信息。 – JLRishe 2013-03-01 14:08:07
雖然只是一個建議,但您可以使用XML-to-XAML,但在基於XAML的技術中,有一個名爲'ItemsControl'的東西,它應該替換所有重複標籤+文本框的東西,給定一個適當的'DataTemplate'和要綁定的數據項。如果你這樣做,我認爲你的XSLT生成的XAML不會像應該那樣可維護。 – 2013-03-01 14:37:50
@JLRishe「您不能先創建DockPanel元素,然後移出它以添加屬性」。他試圖實現的是將某些子元素(可以出現在任何位置)轉換爲父元素中的屬性。如果他在處理所有內容的同時處理這些目標元素之前的某些元素,並且在添加子元素之後XSLT不允許添加屬性。因此,其中一種選擇是使用「to-attr」模式處理所有這些元素,以便可以先處理它們,然後恢復「正常」處理。所以這只是一種排序... – 2013-03-01 15:19:54