如何使用XPath设置ListView控件的ItemSource

时间:2013-03-26 10:24:46

标签: wpf listview xslt wpf-controls xslt-1.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" />

1 个答案:

答案 0 :(得分:0)

只要以正确的方式做到这一点就行了!

    <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>