2009-01-23 59 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

相关问题