コード全体がXSLTを介して自動生成されるため、この質問は少し注意が必要です。これは、いくつかの制限に直面する必要があることを意味します(CodeBehindなどはありません)
XSLTを介してListViewを含むXAMLファイルを正常に生成しました。XAMLファイルにはXMLDataProviderも含まれています。DataSourceとProviderは問題ありませんが、ListViewItemSourceプロパティの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>
このスニペットは最初のアイテムを3回表示します。(データソースに3つのエントリがあるため)他の多くの組み合わせを試しましたが、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" />