当listbox绑定到另一个源时,如何将文本框绑定到列表框内容?

时间:2011-04-27 20:02:10

标签: wpf xaml data-binding

我有一个简单的代码,我正在尝试将TextBlock的Text属性绑定到ListBox但它不起作用! 我试图按照此网站中的说明http://msdn.microsoft.com/en-us/magazine/cc163299.aspx 以下是我的代码:

  <StackPanel>
        <StackPanel.Resources>
            <XmlDataProvider x:Key="MoreColors" XPath="Colors">
                <x:XData>                   
                    <Colors xmlns="">
                        <color name="Green"/>
                        <color name="Blue"/>
                    </Colors>
                </x:XData>
            </XmlDataProvider>
        </StackPanel.Resources>

        <TextBlock Height="23" Name="textBlock1" TextAlignment="Center" 
                       Text="{Binding ElementName=listBox1, Path= SelectedItem.Content, Mode=OneWay}"
                       Background="{Binding Path=Text, RelativeSource={RelativeSource Self}}" Width="119" /> 

        <ListBox x:Name="listBox1" Width="248" Height="56" 
            IsSynchronizedWithCurrentItem="True" 
            ItemsSource="{Binding Source={StaticResource MoreColors}, 
            XPath=color/@name}">
        </ListBox>



    </StackPanel>

我想知道如何解决这个问题。 谢谢 富

2 个答案:

答案 0 :(得分:1)

Path= SelectedItem.Content替换为Path = SelectedItem.Value

答案 1 :(得分:1)

您的数据是XmlAttriute,绑定到InnerText

Text="{Binding ElementName=listBox1, Path=SelectedItem.InnerText, Mode=OneWay}"