如何通过WPF组合框选择从数据库填充列表集合

时间:2012-04-18 14:14:04

标签: c# .net wpf wpf-controls

我有多对多表,想要在WPF控件中显示一些字段。

enter image description here

每当我选择组合框中的InstructorID时,如何填写列表集合List<Course> courses = new List<Course>()?我想在ListView控件中仅显示List中的CourseID和Title

这是我的代码:

public partial class MainWindow : Window
{
    SchoolEntities db = new SchoolEntities();
    public MainWindow()
    {
        InitializeComponent();
        var instructors = db.Instructors.Where(f => f.HireDate == 2011).ToList();

        this.comboBox1.ItemsSource = instructors;
        this.comboBox1.DisplayMemberPath = "LastName";
        this.comboBox1.SelectedValuePath = "InstructorID";           
    }
    List<Course> courses = new List<Course>();

    private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        int S = (int)this.comboBox1.SelectedValue;
        var InstrSelection = db.Instructors.Include("CourseInstructors.Course").SingleOrDefault(f => f.InstructorID == S);


        foreach (var C in InstrSelection.CourseInstructors)
        {
            courses.Add(C.Course);
        }

        this.listView1.DataContext = null;
        this.listView1.DataContext = courses;
    }
}   

窗口xaml

<Window x:Class="School.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="773" Width="677">

<Grid>        
    <ComboBox Height="23" HorizontalAlignment="Left" Name="comboBox1"  SelectionChanged="comboBox1_SelectionChanged"></ComboBox>
    <ListView  HorizontalAlignment="Left" Name="listView1">
        <ListView.View>
         <GridView>
            <GridViewColumn Width="140" Header= "CourseID" />
            <GridViewColumn Width="140" Header= "Title" />                
         </GridView>
        </ListView.View>
    </ListView>
</Grid>

2 个答案:

答案 0 :(得分:0)

我会让我的父数据模型包含子数据模型的集合,并将子ComboBox绑定到父ComboBox

中的选定项目

例如,您的Instructor模型将包含

int InstructorId
string FirstName
string LastName
DateTime HireDate
List Courses

您可以将ComboBox教师绑定到List<Instructor>。然后,您可以将课程ComboBox绑定到教师ComboBox的SelectedItem.Courses

<ComboBox x:Name="InstructorList" ... />

<ComboBox x:Name="CourseList"
          ItemsSource="{Binding ElementName=InstructorList, 
                                Path=SelectedItem.Courses}"
          DisplayMemberPath="Title"
          SelectedValuePath="CourseId" />

答案 1 :(得分:0)

我不确定我明白你在问什么。您是否只是想知道为什么您当前的代码没有在ListView中显示任何课程?如果是这样,那么你的问题是你应该在设置ItemsSource时设置ListView的DataContext。在添加新课程之前,您可能还应该清除课程列表:

private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    courses.Clear();
    ...

    this.listView1.ItemsSource = null; // ItemsSource instead of DataContext
    this.listView1.ItemsSource = courses;
}

修改

您还必须将DisplayMemberBindings添加到GridViewColumns:

<GridViewColumn 
    Width="140" 
    Header="CourseID" 
    DisplayMemberBinding="{Binding CourseID}" /> <!-- Here -->
<GridViewColumn 
    Width="140" 
    Header="Title" 
    DisplayMemberBinding="{Binding Title}" /> <!-- And Here -->