none
请教一个wpf数据绑定的问题 RRS feed

  • 问题

  • <Window x:Class="DataBindingApp2.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
       
        <Window.Resources>
            <XmlDataProvider x:Key="MyList" XPath="/Nationals/*">
                <x:XData>
                    <Nationals xmlns="">
                        <National name="Italia" class="1">
                            <Clubs>
                                <Club cname="Juventus">
                                    <Name>Juventus</Name>
                                    <FoundYear>1896</FoundYear>
                                    <ChampionNumber>29</ChampionNumber>
                                    <Players>
                                        <Player name="Piero" no="10"></Player>
                                        <Player name="Buffon" no="1"></Player>
                                    </Players>
                                </Club>
                                <Club cname="Lecce">
                                    <Name>Lecce</Name>
                                    <FoundYear>1908</FoundYear>
                                    <ChampionNumber>0</ChampionNumber>
                                    <Players>
                                        <Player name="Giacomazzi" no="18"></Player>
                                        <Player name="player2" no="16"></Player>
                                        <Player name="player3" no="21"></Player>
                                    </Players>
                                </Club>
                            </Clubs>
                        </National>
                        <National name="England" class="1">
                            <Clubs>
                                <Club cname="Liverpool">
                                    <Name>Liverpool</Name>
                                    <FoundYear>1892</FoundYear>
                                    <ChampionNumber>18</ChampionNumber>
                                    <Players>
                                        <Player name="Kuyt" no="18"></Player>
                                        <Player name="Gerrard" no="8"></Player>
                                    </Players>
                                </Club>
                                <Club cname="Newcastle United">
                                    <Name>Newcastle United</Name>
                                    <FoundYear>1881</FoundYear>
                                    <ChampionNumber>3</ChampionNumber>
                                    <Players>
                                        <Player name="NU Player01" no="15"></Player>
                                        <Player name="NU Player02" no="5"></Player>
                                        <Player name="NU Player03" no="24"></Player>
                                    </Players>
                                </Club>
                            </Clubs>
                        </National>
                    </Nationals>
                </x:XData>
            </XmlDataProvider>

            <DataTemplate x:Key="NationalTemplate">
                <TextBlock Text="{Binding XPath=@name}" />
            </DataTemplate>

            <DataTemplate x:Key="ClubTemplate">
                <TextBlock Text="{Binding XPath=Name}" />
            </DataTemplate>

            <DataTemplate x:Key="PlayerTemplate">
                <TextBlock Text="{Binding XPath=@name}" />
            </DataTemplate>
        </Window.Resources>

        <DockPanel DataContext="{Binding Source={StaticResource MyList}}">
            <StackPanel>
                <Label>Nation</Label>
                <ListBox ItemsSource="{Binding}"
                     ItemTemplate="{StaticResource NationalTemplate}"
                     IsSynchronizedWithCurrentItem="true"/>
            </StackPanel>

            <StackPanel>
                <Label Content="{Binding XPath=@name}" />
                <ListBox Name="ClubListBox"
                     ItemsSource="{Binding XPath=Clubs/Club}"
                     ItemTemplate="{StaticResource ClubTemplate}"
                     IsSynchronizedWithCurrentItem="true"/>
            </StackPanel>

            <StackPanel>
                <Label Content="{Binding XPath=.//@cname}"/><!--<Label Content="{Binding XPath=.//Name}"/>-->
                <ListBox DataContext="{Binding ElementName=ClubListBox,Path=SelectedItem}"
                     ItemsSource="{Binding XPath=Players/Player}"
                     ItemTemplate="{StaticResource PlayerTemplate}" />
            </StackPanel>
        </DockPanel>
    </Window>

    问题是这样的:当第二个ListBox选项改变的时候,如黑体所示的Label应该显示相应的俱乐部信息,现在却一直显示第一条的信息(即一直显示Juventus,无论是绑定cname属性还是Name结点的值)

    要如何修改才能够显示相应的信息?<Label Content="{Binding XPath=.}"/>下已经取到相关结点的值,但是Label为什么就不会随着ListBox的改变而改变

    请大家帮帮我吧,刚开始学习wpf,先谢谢了!

    2010年9月9日 8:26

全部回复

  • 没这么用过。不过应该是因为有两个ListBox都设置了IsSynchronizedWithCurrentItem,而它们的数据源又不同所致,这样的话collection的CurrentItem就是第一个ListBox选中的National. 第三个Label还是像第三个ListBox一样直接用数据绑定绑到第二个ListBox的SelectedItem上吧。


    Tech Blog: http://gildor.cnblogs.com
    2010年9月14日 8:09
  • 答案已经在你的代码之中了。你的Label如同最后一个ListBox一样设置DataContext即可。

    <Label DataContext="{Binding ElementName=ClubListBox,Path=SelectedItem}" Content="{Binding XPath=@cname}"/>


    Yiling Lai. MVP (Visual C++ and Client App Dev)
    • 已建议为答案 Yiling Lai 2010年10月9日 11:21
    2010年9月27日 12:52
  • 恩,非常谢谢

    2010年10月9日 9:35