为什么XAML元素到xml的绑定只能部分工作?

为什么XAML元素到xml的绑定只能部分工作?,xml,xaml,binding,listbox,Xml,Xaml,Binding,Listbox,在下面的代码中,列表框中填充了XML文件中的颜色名称,但奇怪的是,这些名称没有出现在文本框中 但是,如果将文本框绑定到静态“lbColor2”,则会显示这些名称 那么,当名称来自XML源时,它们会有什么不同,使它们无法传递 <StackPanel> <StackPanel.Resources> <XmlDataProvider x:Key="ExternalColors" Source="App_Data/main.xml" XPath="/

在下面的代码中,列表框中填充了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>

您已经将
文本框
绑定到
SelectedItem.Content
,但是
xmldattribute
没有名为
Content
的属性。换成这个,你会没事的:

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