首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >作为用户控件的WPF数据网格

作为用户控件的WPF数据网格
EN

Stack Overflow用户
提问于 2014-11-18 04:22:13
回答 1查看 250关注 0票数 0

我需要帮助来装订。它的WPF MVVM模式。我已经在UserControl (UC)中放置了一个数据网格和三个ICommand按钮“添加”、“修改”和“删除”按钮。例如,单击UC的5个实例中的任何一个的Add按钮,它都会调用相同的AddStudent函数。

但问题是,5个UC实例必须绑定到5个不同的学生类,工程,医学,建筑等。

这是一个包含学生详细信息的学生实体。

因此,当我单击医学生数据网格的Add按钮时,会弹出一个对话框,询问学生的详细信息,这些信息应该依次添加到医学生的数据网格中。其他4个数据网格也是如此。但问题是,由于所有5个实例都调用相同的AddStudent方法,因此新添加的学生反映在所有5个数据网格中。我希望能够将相应的学生列表绑定到其相应的datagrid控件。PS:我已经将UC的ITemsSource设置为依赖属性。

代码语言:javascript
复制
public class College 
{ 
    private string code; 
    private string name; 

    private Student engineering; 
    private Student medical;
    private Student nursing; 
    private Student architecture; 
    private Student fashionDesign; 

     public Student Engineering 
     { 
         get 
         { 
             return engineering;
         } 
         set 
         { 
            SetField(ref engineering, value, () => Engineering); 
         } 
     } 
 } 

其中,Student是一个类。

EN

回答 1

Stack Overflow用户

发布于 2014-11-19 17:57:16

你是怎么写大学班级的,让人觉得每所大学每种类型的学生只能有一个?一所大学肯定会有一份完整的学生名单,如果你被要求增加更多的课程怎么办?你最终会得到到处都是的网格和一大片混乱。

我的建议是在您的学生类中添加一个StudentType (作为枚举)。

所以我的枚举:

代码语言:javascript
复制
public enum TypeOfStudent
{
    Engineering,
    Medical,
    Computing,
    Fashion     
}

将其作为属性添加到您的学生类中:

代码语言:javascript
复制
public class Student
{
    public TypeOfStudent StudentType;

    //and other details as required...
}

然后,您只需要一份大学班级的学生列表和表单上的一组按钮即可。为了让add student方法知道您想要添加哪种类型的学生,那么在您询问学生信息的表单上添加一个包含枚举值的简单组合框,以便用户从中进行选择。

代码语言:javascript
复制
public class College
{
    //property that contains ALL of the students
    public ObservableCollection<Student> Students

    //property containing the a filtered list of students. Your grid will bind its itemssource to
    //this property. You will need to set this property to equal students on initial load up so
    //it will display all students
    public IEnumerable<Student> FilteredStudents { get { return _filteredStudents; } }

    //property that returns the values in your enum which you can bind a combobox's itemsource to
    public IEnumerable<TypeOfStudent> StudentTypes
    {
        get { return Enum.GetValues(typeof(TypeOfStudent)).Cast<TypeOfStudent>(); }
    }

    //property that stores the selected student type which should be bound to a combobox's
    //selecteditem.
    public TypeOfStudent SelectedStudentType {  get; set + Raise PropertyChanged stuff;  }

    //property storing the selected filter for type of student
    public TypeOfStudent SelectedFilter {get; set + raise propertychanged; }

    //method for adding a student to your list of students
    public void AddStudent()
    {
        this.Students.Add(new Student
        {
            StudentType = this.SelectedStudentType
        });
    }

    //method of applying a filter
    public void FilterStudents()
    {
        _filteredStudents = this.Students.Where(s=>s.StudentType == this.SelectedFilter);
    }
}

至于让你的一个数据网格显示特定类型的学生。如果您为用户提供了一个包含学生类型列表的组合框,那么当他们选择某些内容时,您可以使用filter students方法来更新网格。

现在,我遗漏了一些东西。例如,您可能需要在筛选器中使用某种“全部”选项。你需要做一些提升,属性,改变事件,列表等等,但是我所写的应该会让整个事情变得更容易管理。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26981255

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档