通过 c# wpf 中的 class 类型过滤由可观察集合填充的 lisbox

filter lisbox populated by an observable collection by the class type in c# wpf

我正在创建一个程序,我需要用汽车、自行车和货车填充列表框。我为此创建了 类:

public class Vehicle
    {
        protected string Make { get; set; }
        protected string Model { get; set; }
        public string Price { get; set; }
        protected string Year { get; set; }
        protected string Colour { get; set; }
        public string Mileage { get; set; }
        protected string Description { get; set; }
        protected string Engine { get; set; }

        public Vehicle() { }

        public Vehicle(string make, string model, string price, string colour, string year, string milage, string description, string engine)
        {
            Make = make;
            Model = model;
            Price = price;
            Year = year;
            Mileage = milage;
            Description = description;
            Engine = engine;
            Colour = colour;
        }

        public override string ToString()
        {
            return string.Format("Make:  {0}    Model:  {1} \n Price:  {2}    Mileage:  {3} \n", Make, Model, Price, Mileage);
        }

        public virtual string vehicleDetails()
        {
            return string.Format("Make: {0} \nModel{1} \nPrice {2} \n Year {3} \n Colour: {4}, Mileage: {5} /nDescription: {6} /n Engine: {7}", Make, Model, Price, Year, Colour, Mileage, Description, Engine);
        }

        public virtual string FileFormat()
        {
            return string.Format("{0},{1},{2},{3},{4},{5},{6},{7}", Make, Model, Price, Year, Colour, Mileage, Description, Engine);

        }
    }

    public class Car : Vehicle
    {
        public enum BodyType { Convertible, Hatchback, Coupe, Estate, MPV, SUV, Saloon, Unlisted };
        public BodyType Body { get; set; }

        public Car(string make, string model, string price, string colour, string year, string milage, string description, string engine, BodyType body)
            : base(make, model, price, colour, year, milage, description, engine)
        {
            Body = body;
        }
        public override string vehicleDetails()
        {
            return base.vehicleDetails() + string.Format("body type {0}\n", Body);
        }

        public override string FileFormat()
        {
            return string.Format("{0},{1},{2},{3},{4},{5},{6},{7}, {8}", Make, Model, Price, Year, Colour, Mileage, Description, Engine, Body);
        }
    }

    public class Bike : Vehicle
    {
        public enum BikeType { Scooter, TrailBike, Sports, Commuter, Tourer };
        public BikeType Btype { get; set; }

        public Bike(string make, string model, string price, string colour, string year, string milage, string description, string engine, BikeType bType)
            : base(make, model, price, colour, year, milage, description, engine)
        {
            Btype = bType;
        }
        public override string vehicleDetails()
        {
            return base.vehicleDetails() + string.Format("type {0}\n", Btype);
        }

        public override string FileFormat()
        {
            return string.Format("{0},{1},{2},{3},{4},{5},{6},{7}, {8}", Make, Model, Price, Year, Colour, Mileage, Description, Engine, Btype);
        }
    }

    public class Van : Vehicle
    {
        public enum WheelBase { Short, Medium, Long, Unlisted };
        public WheelBase WBase { get; set; }

        public enum VanType { CombiVan, Dropside, PanelVan, Pickup, Tipper, Unlisted }
        public VanType VType { get; set; }

        public Van(string make, string model, string price, string colour, string year, string milage, string description, string engine, WheelBase wBase, VanType vType)
            : base(make, model, price, colour, year, milage, description, engine)
        {
            WBase = wBase;
            VType = vType;
        }
        public override string vehicleDetails()
        {
            return base.vehicleDetails() + string.Format("wheel base {0}\n Van Type {1} ", WBase, VType);
        }

        public override string FileFormat()
        {
            return string.Format("{0},{1},{2},{3},{4},{5},{6},{7}, {8}", Make, Model, Price, Year, Colour, Mileage, Description, Engine, WBase, VType);
        }
    }
}

然后我将它们全部添加到一个可观察的集合中,(添加将在未来通过用户输入但现在我在代码中添加它们。

Car car4 = new Car("Audi", "A6", "19000", "Red", "20014", "20000", "hannnnndy", "2litre", Car.BodyType.Saloon);

            vehicles.Add(car1);
            vehicles.Add(car2);
            vehicles.Add(car3);
            vehicles.Add(car4);

            Van van1 = new Van("Ford", "transit", "25000", "white", "2008", "100000", "lovely red car", "1.4litre", Van.WheelBase.Medium, Van.VanType.Unlisted);
            Van van2 = new Van("Citroen", "berlingo", "2000", "silver", "2006", "20100", "lovely", "1.4litre", Van.WheelBase.Long, Van.VanType.PanelVan);

然后它们被添加到列表框,我为此使用 lisbox.ItemsSource 但如果需要我可以将其更改为 listbox.Items.Add 然后我必须过滤显示所有车辆、所有汽车、所有货车或所有自行车的列表框。我已经尝试过了。

 private void radioButton_Checked(object sender, RoutedEventArgs e)
    {
        var button = sender as RadioButton;
        string selected = button.Content.ToString();
        if(selected != null)
        {
            if (selected == "all")
            {

            }
            else if(selected == "cars")
            {
                var collectionViewSource = new CollectionViewSource();
                collectionViewSource.Source = vehicles.;
                foreach (object Car in vehicles)
                {

                    lbxVehicles.ItemsSource = CollectionViewSource
                }
            }
            else if(selected == "bikes")
            {
                foreach (object Bike in vehicles)
                {
                    lbxVehicles.Items.Add(Bike);
                }
            }

这些是我认为可以过滤列表框的几种不同方式,但我不知道它们是否可以工作,或者如果我以完全错误的方式进行操作,它也必须通过 a单选按钮。 如果您能解释您可能的解决方案,那就太好了,因为我在这方面不是很先进,并且想尽我所能学习。 提前致谢

您可以使用 LINQ OfType<T>() 通过 Type 过滤 IEnumerable<T>。例如:

var bikes = vehicles.OfType<Bike>().ToList();
var cars = vehicles.OfType<Car>().ToList();