0

这个问题有点棘手,因为整个代码是通过 XSLT 自动生成的。这意味着我必须面对一些限制(没有 CodeBehind 等)

我通过 XSLT 成功生成了一个包含 ListView 的 XAML 文件。XAML 文件还包含一个 XMLDataProvider。DataSource 和 Provider 很好,我只是不知道如何为 ListView ItemSource 属性设置 XPath。

这是我的数据源:

<RelatedContacts>
        <Contact ShowsInterest="true">
            <Name>John</Name>
            <Lastname>Doe</Lastname>
        </Contact >
        <Contact ShowsInterest="true">
            <Name>Max</Name>
            <Lastname>Mustermann</Lastname>
        </Contact >
        <Contact ShowsInterest="true">
            <Name>Claire</Name>
            <Lastname>Grube</Lastname>
        </Contact >
</RelatedContacts>

这是生成的 ListView 代码片段

    <ListView ItemsSource="{Binding XPath=/Contact/RelatedContacts/*}" Name="listview1" DockPanel.Dock="Left, Right, Top, Bottom" Height="125" Background="White" Foreground="Black" Visibility="visible" BorderThickness="1,1,1,1" FontFamily="Tahoma" FontSize="9" FontStyle="Normal" TabIndex="0" IsTabStop="True">
      <ListView.View>
        <GridView>
          <GridViewColumn Header="Name" DisplayMemberBinding="{Binding XPath=../Contact/Name}" />
          <GridViewColumn Header="Lastname" DisplayMemberBinding="{Binding XPath=../Contact/Lastname}" />
        </GridView>
      </ListView.View>
    </ListView>

此代码段将第一项显示三次。(因为数据源中有三个条目)我尝试了很多其他组合,但无法找到可以使用 XSLT 生成的解决方案。例如,这个工作,但我不能用 XSLT 生成它:

<ListView ItemsSource="{Binding XPath=/Contact/RelatedContacts//Contact}" Name="listview1" DockPanel.Dock="Left, Right, Top, Bottom" Height="125" Background="White" Foreground="Black" Visibility="visible" BorderThickness="1,1,1,1" FontFamily="Tahoma" FontSize="9" FontStyle="Normal" TabIndex="0" IsTabStop="True">
  <ListView.View>
    <GridView>
      <GridViewColumn Header="Name" DisplayMemberBinding="{Binding XPath=Name}" />
      <GridViewColumn Header="Lastname" DisplayMemberBinding="{Binding XPath=Lastname}" />
    </GridView>
  </ListView.View>
</ListView>

如前所述,生成了 ListView。这是用 XSLT 转换的“源”片段

  <ListViewWrapper id="listview1" dock="Fill" text="" theme="" width="1078" height="125" backcolor="White" forecolor="Black" visible="True" mapNode="Contact\RelatedContacts" border-left="1" border-top="1" border-right="1" border-bottom="1" font-name="Tahoma" font-size="9" font-style="Regular">
    <TabIndex>0</TabIndex>
    <TabStop>True</TabStop>
    <Columns>
      <Column title="Name" mapNode="Contact\Name" width="0" />
      <Column title="Lastname" mapNode="Contact\Lastname" width="0" />
    </Columns>
  </ListViewWrapper>

在处理 ListViewWrapper 和创建 ListView 时,XSLT 处理器不知道mapNode列的元素,因为层次结构更深。(我确信有一种方法,但我不知道该怎么做。)此外,列也有可能映射到这样的不同元素。

<Columns>
  <Column title="Name" mapNode="Contact\Name" width="0" />
  <Column title="Lastname" mapNode="BusinessContact\Lastname" width="0" />
</Columns>

总结一下,这就是我试图实现的目标:

ItemsSource="{Binding XPath=/Contact/RelatedContacts//*}

接着

<GridViewColumn Header="Name" DisplayMemberBinding="{Binding XPath=Name}" />

在 ListView 中显示 RelatedContacts 的所有子元素,而不显式定义整个路径。我正在寻找类似占位符的东西。如果没有这个条件,它看起来像XPath=/Contact/RelatedContacts//Contact.

对于感兴趣的人,这里提供了 XSLT 样式表的一部分:

 <!-- Transformiere ListViewWrapper zu ListView -->
  <xsl:template match="ListViewWrapper">
    <xsl:element name="ListView">
      <xsl:attribute name="ItemsSource">
        <xsl:variable name="binding-path" select="./@mapNode"/>
        <xsl:variable name="bindpath" select="translate($binding-path, '\','/')" />
        <xsl:value-of select="concat('{Binding XPath=/',$bindpath,'/*}')"/>
      </xsl:attribute>
      <xsl:apply-templates select="@*|*" mode="to-attr" />
      <xsl:element name="ListView.View">
        <xsl:apply-templates select="*" />
      </xsl:element>
    </xsl:element>
  </xsl:template>

  <!-- ListView: Transformieren von Columns (Wrapper) zu GridView -->
  <xsl:template match="Columns">
    <xsl:element name="GridView">
      <xsl:apply-templates select="*" />
    </xsl:element>
  </xsl:template>

  <!-- ListView: Transformieren von Column zu GridViewColumn -->
  <xsl:template match="Column">
    <xsl:variable name="binding-path" select="./@mapNode"/>
    <xsl:element name="GridViewColumn">
      <xsl:apply-templates select="@*" />
      <xsl:attribute name="Header">
        <xsl:value-of select="./@title" />
      </xsl:attribute>
      <xsl:attribute name="DisplayMemberBinding">
        <xsl:variable name="bindpath" select="translate($binding-path, '\','/')" />
        <xsl:value-of select="concat('{Binding XPath=../',$bindpath,'}')"/>
      </xsl:attribute>
      <xsl:call-template name="listbox-width"/>
    </xsl:element>
  </xsl:template>

  <!-- ListView: Setzen der Column width. Falls 0 dann nichts angeben (auto size) -->
  <xsl:template match="width" name="listbox-width">
    <xsl:if test="./@width != 0">
      <xsl:attribute name="Width">
        <xsl:value-of select="./@width" />
      </xsl:attribute>
    </xsl:if>
  </xsl:template>

  <!-- Ausschluss des ListView Width Attribut -->
  <xsl:template match="ListViewWrapper/@width"
              mode="to-attr" />
4

1 回答 1

0

Just do it the right way an it will work!

    <ListView ItemsSource="{Binding XPath=/Contact/RelatedContacts/*}" Name="listview1" DockPanel.Dock="Left, Right, Top, Bottom" Height="125" Background="White" Foreground="Black" Visibility="visible" BorderThickness="1,1,1,1" FontFamily="Tahoma" FontSize="9" FontStyle="Normal" TabIndex="0" IsTabStop="True">
      <ListView.View>
        <GridView>
          <GridViewColumn Header="Name" DisplayMemberBinding="{Binding XPath=Name}" />
          <GridViewColumn Header="Lastname" DisplayMemberBinding="{Binding XPath=Lastname}" />
          <GridViewColumn Header="Age" DisplayMemberBinding="{Binding XPath=Age}" />
          <GridViewColumn Header="Street" DisplayMemberBinding="{Binding XPath=Street}" />
          <GridViewColumn Header="City" DisplayMemberBinding="{Binding XPath=City}" />
          <GridViewColumn Header="Postal code" DisplayMemberBinding="{Binding XPath=PostalCode}" />
          <GridViewColumn Header="Country" DisplayMemberBinding="{Binding XPath=Country}" />
        </GridView>
      </ListView.View>
    </ListView>
于 2013-03-26T15:02:52.423 回答