简体   繁体   English

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

[英]How to fill a list collection from a database, by WPF combobox selection

I have Many-to-Many Tables and want to display some of the fields in WPF controls. 我有多对多表,并希望显示WPF控件中的某些字段。

在此处输入图片说明

How can I fill the list collection List<Course> courses = new List<Course>() whenever I select the InstructorIDs in the combobox ? 每当我在组合框中选择InstructorID时,如何填充列表集合List<Course> courses = new List<Course>() I want to display only the CourseID and Title from the List in the ListView control 我只想显示ListView控件中列表中的CourseID和Title

Here're my codes: 这是我的代码:

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;
    }
}   

And the window xaml 和窗口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>

I would make my parent data model contain a collection of the child data model, and bind the child ComboBox to the selected item in the parent ComboBox 我将使父数据模型包含子数据模型的集合,并将子ComboBox绑定到父ComboBox的选定项。

For example, your Instructor model would contain 例如,您的Instructor模型将包含

int InstructorId
string FirstName
string LastName
DateTime HireDate
List Courses

and you would bind your ComboBox of instructors to a List<Instructor> . 然后将您的教师ComboBox绑定到List<Instructor> Then you could bind your courses ComboBox to the instructor ComboBox's SelectedItem.Courses 然后您可以将您的课程ComboBox绑定到讲师ComboBox的SelectedItem.Courses

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

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

I am not sure I understand what you are asking. 我不确定我是否理解您的要求。 Are you just wondering why your current code is not displaying any courses in the ListView? 您是否在想为什么您当前的代码未在ListView中显示任何课程? If so then your problem is that you are setting the DataContext of the ListView when you should be setting the ItemsSource. 如果是这样,那么您的问题就是您应该在设置ItemsSource时设置ListView的DataContext。 You should likely also be clearing the list of courses before adding new ones: 您可能还应该在添加新课程之前清除课程列表:

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

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

Edit 编辑

You will also have to add the DisplayMemberBindings to your GridViewColumns: 您还必须将DisplayMemberBindings添加到GridViewColumns中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM