案例地址:GitHub - microsoft/WPF-Samples: Repository for WPF related samples

一、运行界面

 二、案例功能描述

实现主明细式图表应用(主从表)【master-detail】

知识点:

1、通过在最上级设置上下文后,再逐级绑定实现主从关系。

其中Selector. IsSynchronizedWithCurrentItem="true"

 

2、 ItemsSource="{Binding}" 表示绑定的是上下文

三、分析代码

1、在最上级设置上下文,在数据绑定时逐级绑定

 <DockPanel DataContext="{Binding Source={StaticResource MyList}}">
        <StackPanel>
            <Label>My Soccer Leagues</Label>
            <ListBox ItemsSource="{Binding}"
               IsSynchronizedWithCurrentItem="true"/>
        </StackPanel>

        <StackPanel>
            <Label Content="{Binding Path=Name}"/>
            <ListBox ItemsSource="{Binding Path=Divisions}"
               IsSynchronizedWithCurrentItem="true"/>
        </StackPanel>
        <StackPanel>
            <Label Content="{Binding Path=Divisions/Name}"/>
            <ListBox ItemsSource="{Binding Path=Divisions/Teams}"/>
        </StackPanel>
    </DockPanel>

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐