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