【问题标题】:How to fill a list collection from a database, by WPF combobox selection如何通过 WPF 组合框选择从数据库中填充列表集合
【发布时间】:2012-04-18 14:14:03
【问题描述】:

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

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

【问题讨论】:

    标签: c# .net wpf wpf-controls


    【解决方案1】:

    我不确定我是否理解您的要求。您是否只是想知道为什么您当前的代码没有在 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 -->
    

    【讨论】:

    • 我的答案的第一部分有帮助吗?
    • 我使用了 ItemSource,它在 ListView 列中显示了我的命名空间 School.Course。有什么我想念的吗?
    【解决方案2】:

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

    例如,您的 Instructor 模型将包含

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

    并且您会将您的 ComboBox 讲师绑定到 List&lt;Instructor&gt;。然后你可以将你的课程ComboBox绑定到导师ComboBox的SelectedItem.Courses

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

    【讨论】:

      猜你喜欢
      • 2020-10-28
      • 2015-01-03
      • 1970-01-01
      • 2012-02-09
      • 1970-01-01
      • 1970-01-01
      • 2012-09-11
      • 1970-01-01
      相关资源
      最近更新 更多