Listview显示ObservableSelection [英] Listview to display ObservableSelection

查看:140
本文介绍了Listview显示ObservableSelection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Student表,课程表和StudentCourse表。在我的WPF,我正在做学生选择与组合框。如何使用ObservableCollection在列表视图中显示相应的课程及其属性(CourseID,CourseName,Credit)?这是我的组合框SelectionChanged事件的代码

I have Student table, Course table and a StudentCourse table. On my WPF, I'm making "Student" selections with combo box. How can I display the corresponding Course and its properties(CourseID, CourseName, Credit) in a listview, using ObservableCollection?. Here's my code for the combo box SelectionChanged event

 private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        int MySelect = (int)this.comboBox1.SelectedValue;
        var SelectedStudent = db.Students.Include("StudentCourses.Course").SingleOrDefault(f => f.StudentID == MySelect);


推荐答案

如果你还没有,使用MVVM风格的方法。您的ListView的ItemsSource应绑定到ViewModel中的ObservableCollection,并且您的ComboBox的SelectedItem也应绑定到ViewModel上。当SelectedItem更改并调用属性的setter更新您的ListView绑定到的ObservableCollection。

If you're not already, I would highly recommend using a MVVM styled approach. Your ListView's ItemsSource should be bound to an ObservableCollection in your ViewModel and your ComboBox's SelectedItem should also be bound to a propety on your ViewModel. When the SelectedItem changes and calls the property's setter update the ObservableCollection that your ListView is bound to.

更新:

这是部分实现的解决方案:

Here's a partially implemented solution:

XAML:

<DockPanel>
    <ComboBox ItemsSource="{Binding Students}" SelectedItem="{Binding SelectedComboItem}" DockPanel.Dock="Top">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}"/>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

    <ListView ItemsSource="{Binding StudentCourses}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="{Binding Name, StringFormat={}Name: {0}}"/>
                    <TextBlock Text="{Binding Id, StringFormat={}Id: {0}}"/>
                    <TextBlock Text="{Binding Credit, StringFormat={}Credit: {0}}"/>
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</DockPanel>

代码:

public class Student
{
    public string Name { get; set; }
    public List<string> CourseIds { get; set; }
} 


public class Course
{
    public string Name { get; set; }
    public string Id { get; set; }
    public int Credit { get; set; }
}


public class ViewModel : INotifyPropertyChanged
{
    public ObservableCollection<Course> StudentCourses { get; set; }
    public ObservableCollection<Student> Students { get; set; }


    public Student SelectedComboItem
    {
        get { return selectedComboItem_; }
        set {
            selectedComboItem_ = value;
            StudentCourses.Clear();
            foreach(Course course in courses_)
                if(selectedComboItem_.CourseIds.Contains(course.Id))
                    StudentCourses.Add(course);
            PropertyChanged(this, new PropertyChangedEventArgs("SelectedComboItem"))    ;
        }
    }


    private List<Course> courses_ = new List<Course>();
    private Student selectedComboItem_;


    ... // Read DB and build collections

    public event PropertyChangedEventHandler PropertyChanged;
}

这篇关于Listview显示ObservableSelection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆