2013-05-19 48 views
1

我尝试使用XSL创建一个XAML文件,并且我需要的其中一项是100个文本块的唯一名称。我在一个for-each循环创建的TextBlocks(工作,所有元素都被创建),然后尝试使用位置()给每一个独特的名字:XSL XAML独特名称

<xsl:for-each select="//value"> 
    <xsl:element name="TextBlock"> 
    <xsl:attribute name="x:Name" select="'number_txt_',position()"/> 
    <xsl:attribute name="Grid.Row" select="position()+2"/> 
    <xsl:attribute name="Grid.Column" select="0"/> 
    <xsl:attribute name="Text" select="./@number"/> 
    <xsl:attribute name="FontSize" select="20"/> 
    <xsl:attribute name="Foreground" select="'Ivory'"/> 
    <xsl:attribute name="HorizontalAlignment"> 
     <xsl:value-of select="'Center'"/> 
    </xsl:attribute> 
    <xsl:attribute name="VerticalAlignment"> 
    <xsl:value-of select="'Center'"/> 
    </xsl:attribute> 
</xsl:element> 
</xsl:for-each> 

然而,这给了我这样的:

<TextBlock x:Name="number_txt_ 1" Grid.Row="3" Grid.Column="0" Text="1" FontSize="20" 
      Foreground="Ivory" 
      HorizontalAlignment="Center" 
      VerticalAlignment="Center"/> 
<TextBlock x:Name="number_txt_ 2" Grid.Row="4" Grid.Column="0" Text="2" FontSize="20" 
      Foreground="Ivory" 
      HorizontalAlignment="Center" 
      VerticalAlignment="Center"/> 

等所有的TextBlocks。请注意“number_txt_”和数字之间的空格。我想在C#Silverlight项目中使用此文件,但不允许在ax:Name中使用空格,也不允许使用单个数字(我只用计数器尝试过,不起作用)。 有没有人有什么想法?我知道你们中的一些人会提出一个计数器,但我对此的了解很少。 我感谢你,百忙之中阅读我的问题的时候,希望你能想到的一个解决方案。

回答

1

替换此

<xsl:attribute name="x:Name" select="'number_txt_',position()"/> 

<xsl:attribute name="x:Name" select="concat('number_txt_',position())"/> 

此外,此整个片段

<xsl:element name="TextBlock"> 
<xsl:attribute name="x:Name" select="'number_txt_',position()"/> 
<xsl:attribute name="Grid.Row" select="position()+2"/> 
<xsl:attribute name="Grid.Column" select="0"/> 
<xsl:attribute name="Text" select="./@number"/> 
<xsl:attribute name="FontSize" select="20"/> 
<xsl:attribute name="Foreground" select="'Ivory'"/> 
<xsl:attribute name="HorizontalAlignment"> 
    <xsl:value-of select="'Center'"/> 
</xsl:attribute> 
<xsl:attribute name="VerticalAlignment"> 
<xsl:value-of select="'Center'"/> 
</xsl:attribute> 

可以重新写在一个更短的和理解的形式

<TextBlock x:Name="number_txt_{position()}" Grid.Row="{position()+2}" 
      Grid.Column="0" Text="{@number}" FontSize="20" Foreground="Ivory" 
      HorizontalAlignment="Center" VerticalAlignment="Center"> 
+0

就像一个魅力。非常感谢你! – Stybar

+0

@Stybar,欢迎您。 –