天天看点

WPF三级联动

WPF实现三级联动Grade-->Class-->Student,数据来自Access,采用LinqToAccess,已封装成DLL,数据库在附件中。

见图:

WPF三级联动
WPF三级联动

Code:

  1. <Windowx:Class="Demo16_MasterDetailBinding2.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:local="clr-namespace:Demo16_MasterDetailBinding2"
  5. xmlns:dl="clr-namespace:DataLibrary;assembly=DataLibrary"
  6. Title="MainWindow"Height="350"Width="525">
  7. <Window.Resources>
  8. <ObjectDataProviderx:Key="gradeList"IsAsynchronous="True"
  9. ObjectType="{x:Typelocal:MainWindow}"
  10. MethodName="LoadData"/>
  11. </Window.Resources>
  12. <GridDataContext="{StaticResourcegradeList}">
  13. <Grid.Resources>
  14. <StyleTargetType="TextBlock">
  15. <SetterProperty="Margin"Value="5"/>
  16. </Style>
  17. <StyleTargetType="ListBox">
  18. <SetterProperty="Margin"Value="5"/>
  19. </Style>
  20. </Grid.Resources>
  21. <Grid.ColumnDefinitions>
  22. <ColumnDefinition/>
  23. <ColumnDefinition/>
  24. <ColumnDefinition/>
  25. </Grid.ColumnDefinitions>
  26. <Grid.RowDefinitions>
  27. <RowDefinitionHeight="Auto"/>
  28. <RowDefinition/>
  29. </Grid.RowDefinitions>
  30. <TextBlockText="Grade"Grid.Row="0"Grid.Column="0"/>
  31. <TextBlockText="Class"Grid.Row="0"Grid.Column="1"/>
  32. <TextBlockText="Student"Grid.Row="0"Grid.Column="2"/>
  33. <ListBoxGrid.Row="1"Grid.Column="0"IsSynchronizedWithCurrentItem="True"ItemsSource="{Binding}"DisplayMemberPath="GradeName"/>
  34. <ListBoxGrid.Row="1"Grid.Column="1"IsSynchronizedWithCurrentItem="True"ItemsSource="{BindingPath=Class}"DisplayMemberPath="ClassName"/>
  35. <ListBoxGrid.Row="1"Grid.Column="2"IsSynchronizedWithCurrentItem="True"ItemsSource="{BindingPath=Class/Student}"DisplayMemberPath="StudentName"/>
  36. </Grid>
  37. </Window>

Code:

  1. usingSystem.Linq;
  2. usingSystem.Windows;
  3. usingDataLibrary;
  4. namespaceDemo16_MasterDetailBinding2{
  5. ///<summary>
  6. ///MainWindow.xaml的交互逻辑
  7. ///</summary>
  8. publicpartialclassMainWindow:Window{
  9. publicMainWindow(){
  10. InitializeComponent();
  11. }
  12. ///<summary>
  13. ///加载数据,请注意使用publicstatic
  14. ///</summary>
  15. publicstaticIQueryableLoadData(){
  16. MySchoolDataContextdc=Linq2AccessFactory.GetMySchoolDataContext();
  17. varquery=fromgindc.Grade
  18. selectg;
  19. returnquery;
  20. }
  21. }
  22. }

共享链接:http://download.csdn.net/detail/qing2005/4001862