0

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

在此处输入图像描述

List<Course> courses = new List<Course>()每当我在组合框中选择 InstructorID 时,如何填写列表集合?我只想在 ListView 控件的列表中显示 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>

4

2 回答 2

0

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

例如,您的Instructor模型将包含

int InstructorId
字符串名字
字符串姓氏
日期时间雇用日期
列出课程

并且您会将您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" />
于 2012-04-18T14:31:12.347 回答
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 -->
于 2012-04-18T14:32:59.623 回答