1

I have a List called VehicleList and an IEnumerable list call FileredVehicleList. I fill the VehicleList with class and their properties and put them in a listbox, When the user switches the option From all to any of the options I want the listbox to show vehicles with only that class of vehicle in it.

So my question is how do I get the VehicleList, sort it, take out only the vehicles the user wants and put it into FilteredVehicleList to display in the listbox?

Code:

 public List<Vehicle> VehicleList = new List<Vehicle>();
 public IEnumerable<Vehicle> FilteredVehicleList = new List<Vehicle>();

 Vehicle v1 = new Car() { Make = "Ford", Model = "Fiesta", Price = 10000, Year = "1999", Colour = "Red", Mileage = 50000, Description = "Lovely red car, 4 wheel, optional steering wheel.", VehicleType = "Car"};
        VehicleList.Add(v1);
        Vehicle v2 = new Car() { Make = "Ford", Model = "Mondeo", Price = 20000, Year = "2001", Colour = "Green", Mileage = 50000, Description = "Lovely green car, 4 wheel, optional steering wheel.", VehicleType = "Car" };
        VehicleList.Add(v2);
        Vehicle v3 = new Car() { Make = "Ford", Model = "Fiesta", Price = 15000, Year = "2003", Colour = "Silver", Mileage = 50000, Description = "Lovely silver car, 4 wheel, optional steering wheel.", VehicleType = "Car" };
        VehicleList.Add(v3);
        Vehicle v4 = new Car() { Make = "Opel", Model = "Astra", Price = 25000, Year = "2002", Colour = "Silver", Mileage = 50000, Description = "Lovely silver car, 4 wheel, optional steering wheel.", VehicleType = "Car" };
        VehicleList.Add(v4);
        Vehicle v5 = new Bike() { Make = "Kawasaki", Model = "ZX", Price = 30000, Year = "2008", Colour = "Black", Mileage = 25000, Description = "Lovely black bike, 2 wheel, no optional steering wheel.", VehicleType = "Bike" };
        VehicleList.Add(v5);

string[] filterBox = { "All", "Price", "Mileage", "Make" };
        comboBox_sortCars.ItemsSource = filterBox;
        comboBox_sortCars.SelectedIndex = 0;

        FilteredVehicleList = VehicleList;


    private void comboBox_sortCars_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var output = listbox_cars.ItemsSource = "";
        var filter;
        string sortBy = comboBox_sortCars.SelectedValue.ToString();

        switch (sortBy)
        {
            case "Make":
                filter = FilteredVehicleList.VehicleType.OrderBy(i => i.Make)

                break;
        }
J.Gold
  • 55
  • 7
  • XAML-based programs should use `CollectionViewSource` to control the visual presentation, including filtering and sorting. See also e.g. https://stackoverflow.com/questions/20888619/proper-way-to-use-collectionviewsource-in-viewmodel and https://stackoverflow.com/questions/1280704/how-can-i-sort-a-listbox-using-only-xaml-and-no-code-behind – Peter Duniho Feb 25 '17 at 02:52

0 Answers0