WPF实现三级联动Grade-->Class-->Student,数据来自Access,采用LinqToAccess,已封装成DLL,数据库在附件中。
见图:
Code:
- <Windowx:Class="Demo16_MasterDetailBinding2.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="clr-namespace:Demo16_MasterDetailBinding2"
- xmlns:dl="clr-namespace:DataLibrary;assembly=DataLibrary"
- Title="MainWindow"Height="350"Width="525">
- <Window.Resources>
- <ObjectDataProviderx:Key="gradeList"IsAsynchronous="True"
- ObjectType="{x:Typelocal:MainWindow}"
- MethodName="LoadData"/>
- </Window.Resources>
- <GridDataContext="{StaticResourcegradeList}">
- <Grid.Resources>
- <StyleTargetType="TextBlock">
- <SetterProperty="Margin"Value="5"/>
- </Style>
- <StyleTargetType="ListBox">
- <SetterProperty="Margin"Value="5"/>
- </Style>
- </Grid.Resources>
- <Grid.ColumnDefinitions>
- <ColumnDefinition/>
- <ColumnDefinition/>
- <ColumnDefinition/>
- </Grid.ColumnDefinitions>
- <Grid.RowDefinitions>
- <RowDefinitionHeight="Auto"/>
- <RowDefinition/>
- </Grid.RowDefinitions>
- <TextBlockText="Grade"Grid.Row="0"Grid.Column="0"/>
- <TextBlockText="Class"Grid.Row="0"Grid.Column="1"/>
- <TextBlockText="Student"Grid.Row="0"Grid.Column="2"/>
- <ListBoxGrid.Row="1"Grid.Column="0"IsSynchronizedWithCurrentItem="True"ItemsSource="{Binding}"DisplayMemberPath="GradeName"/>
- <ListBoxGrid.Row="1"Grid.Column="1"IsSynchronizedWithCurrentItem="True"ItemsSource="{BindingPath=Class}"DisplayMemberPath="ClassName"/>
- <ListBoxGrid.Row="1"Grid.Column="2"IsSynchronizedWithCurrentItem="True"ItemsSource="{BindingPath=Class/Student}"DisplayMemberPath="StudentName"/>
- </Grid>
- </Window>
Code:
- usingSystem.Linq;
- usingSystem.Windows;
- usingDataLibrary;
- namespaceDemo16_MasterDetailBinding2{
- ///<summary>
- ///MainWindow.xaml的交互逻辑
- ///</summary>
- publicpartialclassMainWindow:Window{
- publicMainWindow(){
- InitializeComponent();
- }
- ///<summary>
- ///加载数据,请注意使用publicstatic
- ///</summary>
- publicstaticIQueryableLoadData(){
- MySchoolDataContextdc=Linq2AccessFactory.GetMySchoolDataContext();
- varquery=fromgindc.Grade
- selectg;
- returnquery;
- }
- }
- }
共享链接:http://download.csdn.net/detail/qing2005/4001862