2016-03-20 5 views
1

Ich erstelle ein Programm, wo ich eine Listbox mit Autos, Fahrrädern und Lieferwagen bevölkern muss. Ich habe Klassen für diese.filter lisbox bevölkert von einer beobachtbaren sammlung durch die klassenart 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); 
     } 
    } 
} 

ich dann, sie alle zu einer beobachtbaren Sammlung hinzufügen, (die Zugabe von in der Zukunft über eine Benutzereingabe wird aber jetzt addiere ich sie in Code

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); 

Sie werden dann zu einem listboux hinzugefügt, ich habe lisbox.ItemsSource dafür verwendet, aber ich kann dies in listbox.Items.Add ändern, wenn nötig Ich muss dann die Listbox filtern, die entweder alle Fahrzeuge, alle Autos anzeigt , alle Vans, oder alle Fahrräder. Ich habe versucht, dies.

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); 
       } 
      } 

Dies sind ein paar verschiedene Möglichkeiten, die ich dachte, könnte man möglicherweise die Listbox filtern, aber ich weiß nicht, ob sie funktionieren können oder wenn ich es komplett falsch mache, es muss auch gefiltert werden über ein Radio knopf. Wenn Sie Ihre möglichen Lösungen erklären könnten, wäre das großartig, weil ich nicht sehr fortgeschritten bin, wenn es darum geht, und möchte alles lernen, was ich kann. Vielen Dank im Voraus

Antwort

1

Sie können LINQ OfType<T>() verwenden, um einen IEnumerable<T> von Type zu filtern. Zum Beispiel:

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

Arbeitete perfekt und ist leicht zu verstehen. Vielen Dank. – Kevin