2009-01-23 60 views
0

在下面的代碼中,ListBox被填充來自XML文件的顏色名稱,但這些名稱奇怪地不出現在TextBox中。爲什麼XAML element-to-xml綁定只能部分工作?

但是,如果將文本框綁定到靜態「lbColor2」,則會出現這些名稱。

那麼,當它們來自XML源代碼時,名稱可能會因名稱不同而有所不同?

<StackPanel> 
    <StackPanel.Resources> 
     <XmlDataProvider x:Key="ExternalColors" Source="App_Data/main.xml" XPath="/colors"/> 
    </StackPanel.Resources> 
    <TextBlock Text="Colors:"/> 
    <ListBox Name="lbColor" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Source={StaticResource ExternalColors}, XPath=color/@name}"/> 
    <ListBox Name="lbColor2"> 
     <ListBoxItem>Red</ListBoxItem> 
     <ListBoxItem>Orange</ListBoxItem> 
     <ListBoxItem>Cyan</ListBoxItem> 
    </ListBox> 
    <TextBlock Text="You selected color:"/> 
    <TextBox 
     Text="{Binding ElementName=lbColor, Path=SelectedItem.Content}" 
     > 
    </TextBox> 
</StackPanel> 

下面是XML文件:

<?xml version="1.0" encoding="utf-8" ?> 
<colors> 
    <color name="Pink"/> 
    <color name="Cyan"/> 
    <color name="LightBlue"/> 
    <color name="LightGreen"/> 
    <color name="Another One"/> 
</colors> 

回答

1

你已經綁定的TextBoxSelectedItem.Content,但XmlAttribute沒有一個屬性叫做Content。更改爲此,你會沒事的:

<TextBox Text="{Binding ElementName=lbColor, Path=SelectedItem.Value}"/> 
+0

是的,無論是或Path = SelectedValue的作品,謝謝! – 2009-01-23 15:39:44

相關問題