如何在第一行中使用过滤器创建datagrid?

时间:2018-02-26 19:52:47

标签: c# wpf mvvm datagrid

我想将此数据网格用于不同的模型。我使用ItemsSource添加项目和生成列。 例如,我添加了照片。enter image description here

视图

$field.select2({ 
  data: response,
  tags: true,
});

模型车

public partial class MainWindow : Window
    {

        string[] categorys = { "person", "car" };
        public MainWindow()
        {
            InitializeComponent();
            category.ItemsSource = categorys;
        }



        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            switch (category.SelectedItem)
            {
                case "person":
                    ViewModel.viewModelPerson model = new ViewModel.viewModelPerson();
                    this.datagrid.ItemsSource = model.MyPerson;
                    break;
                case "car":
                    ViewModel.viewModelCar car = new ViewModel.viewModelCar();
                    this.datagrid.ItemsSource = car.MyCar;
                    break;
            }
        }
    }

ModelPerson

class Car
    {
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        private string model;

        public string Model
        {
            get { return model; }
            set { model = value; }
        }
        public Car(string name,string model)
        {
            this.model = model;
            this.name = name;
        }
    }

viewModelCar

class Person
    {
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        private string last_name;

        public string LastName
        {
            get { return last_name; }
            set { last_name = value; }
        }
        private int age;

        public int Age
        {
            get { return age; }
            set { age = value; }
        }
        public Person(string name , string lastName, int age)
        {
            this.name = name;
            this.last_name = lastName;
            this.age = age;
        }

    }

viewModelCode

 public List<Car> MyCar
        {
            get;
            set;
        }

        public viewModelCar()
        {
            createCar();

        }
        void createCar()
        {
            MyCar = new List<Car>();
            MyCar.Add(new Car("toyota", "corolla"));
            MyCar.Add(new Car("skoda", "octavia"));

        }

结果我得到了 enter image description here

当用户选择类别时,内容会在网格中更改。 enter image description here

如果我在dataGrid中有很多数据,那对用户来说就不好了。这就是我想添加类似于图像enter image description here

的过滤器的原因

0 个答案:

没有答案