views:

230

answers:

1

In the below code, the ListBox gets filled with the names of the colors from the XML file, but those names strangely do not appear in the TextBox.

However, if you bind the textbox to the static "lbColor2", those names do appear.

So what could be different about the names when they come from the XML source which makes them not get passed on?

<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>

Here is the XML file:

<?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  A: 

You've bound the TextBox to SelectedItem.Content, but XmlAttribute does not have a property called Content. Change to this and you'll be fine:

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

HTH, Kent

Kent Boogaart
yes, either that or Path=SelectedValue works, thanks!
Edward Tanguay