2010-04-16 52 views
1

對不起,我剛開始使用wpf編程。我似乎無法弄清楚爲什麼下面的xaml顯示「System.Xml.XmlElement」,而不是實際的XML節點內容。每當我運行它時,它會在列表框中顯示5次。不知道我要去哪裏錯...XmlDataProvider轉換器問題

<Window x:Class="TestBinding.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Window1" Height="300" Width="300"> 
    <Window.Resources> 
     <XmlDataProvider x:Key="myXmlSource" XPath="/root"> 
      <x:XData> 
       <root xmlns=""> 
        <name>Steve</name> 
        <name>Arthur</name> 
        <name>Sidney</name> 
        <name>Billy</name> 
        <name>Steven</name> 
       </root> 
      </x:XData> 
     </XmlDataProvider> 
     <DataTemplate x:Key="shmooga"> 
      <TextBlock Text="{Binding}"/> 
     </DataTemplate> 
    </Window.Resources> 
    <Grid> 
     <ListBox ItemTemplate="{StaticResource shmooga}" 
       ItemsSource="{Binding Source={StaticResource myXmlSource}, XPath=name}"> 
     </ListBox> 
    </Grid> 
</Window> 

任何幫助將非常感激。謝謝!

回答

0

Your ItemsSource Binding正在返回'name'元素的集合。這些是XmlElement類型。這樣做是因爲它允許綁定獲取XmlElement的其他屬性,但意味着如果您以文本方式顯示綁定的結果,那麼您得到System.Xml.XmlElement而不是文本內容。

要獲得的文本內容,添加一個額外的XPath您的ItemTemplate綁定到指定的TextBlock.Text屬性應該特異性結合的元素,而不是元素對象本身的文本:

<DataTemplate x:Key="shmooga"> 
    <TextBlock Text="{Binding XPath=text()}"/> <!-- Note XPath on Binding --> 
</DataTemplate> 
+0

這是偉大的!謝謝!我真的很難過這個。 – Andrew 2010-04-16 23:46:28