我创建了一个程序,我需要用汽车,自行车和面包车填充列表框。我为此创建了类:
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);
然后他们被添加到listboux,我正在使用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);
}
}
这些是我认为有可能过滤列表框的几种不同方式,但我不知道它们是否可以工作,或者我是否完全以错误的方式处理它,它也必须通过VIA过滤单选按钮。 如果你能解释一下你可能的解决方案,那将是很好的,因为我在这方面并不是很先进,并希望尽我所能。 提前谢谢
答案 0 :(得分:1)
您可以使用LINQ OfType<T>()
按IEnumerable<T>
过滤Type
。例如:
var bikes = vehicles.OfType<Bike>().ToList();
var cars = vehicles.OfType<Car>().ToList();