此問題有點棘手,因爲整個代碼是通過XSLT自動生成的。這意味着我必須面對一些限制(無CodeBehind等)如何使用XPath設置ListView控件的ItemSource
我成功生成了包含通過XSLT的ListView的XAML文件。 XAML文件還包含一個XMLDataProvider。數據源和提供者都很好,我只是不知道如何設置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和創建XSLT處理器沒有關於列,因爲有更深層次的mapNode
元素知識ListView的「源」片段。 (我確信有一種方法,但我不知道該怎麼做。)此外,列還可以映射到不同的元素。
<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" />