我正在创建一个程序,我需要用汽车、自行车和货车填充列表框。我为此创建了类:
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。项目来源,但我可以将其更改为列表框。项目.添加(如果需要)然后,我必须过滤显示所有车辆、所有汽车、所有货车或所有自行车的列表框。我已经在这方面进行了尝试。
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);
}
}
这些是我认为可以过滤列表框的几种不同方法,但我不知道它们是否可以工作,或者我是否完全以错误的方式进行,它还必须通过单选按钮进行过滤。如果您能解释一下您可能的解决方案,那就太好了,因为我在这方面不是很先进,想尽我所能学习。提前致谢
LINQ OfType<T>()
按Type
筛选IEnumerable<T>
。例如:
var bikes = vehicles.OfType<Bike>().ToList();
var cars = vehicles.OfType<Car>().ToList();