0

Let's say I have an interface called ICar and it is implemented by two classes; BMW and Audi. They contain the name and the content (visualisation of the car). I want to get the names "BMW" and "Audi" and listed them in Combo Box. So once the user selects the name the object content should appear. This what I've done so far.

interface ICar{
    String Name {get;}
    object Content{get;}
}

It is inherited by two classes, Audi and BMW.

class Audi : ICar{
    AudiCar audi = new AudiCar();

    String Name{get {return"Audi";}}
    object Content{get {return audi;}}
}


class BMW : ICar{
    BMWCar bmw = new BMWCar();

    String Name{get {return"BMW";}}
    object Content{get {return bmw;}}
}

BMWCar and AudiCar have the visualisation of BMW and Audi car.

In MainModel class,

Class MainModel{
    List<ICar> cars;
    ICar selectedCar;

    MainModel(IEnumerable<ICar> cars){
        this.cars = new List<ICar>(cars);
        this.selectedCar = this.cars.FirstOrDefault();
    } 

    IList<ICar> Cars {get{return this.cars;}}

    public object Car{
        get{ return this.selectedCar.Content;}
    }
}

In the XAML,

xmlns:local="clr-namespace:WpfApplication1"

<ComboBox ItemsSource="{Binding Cars}" DisplayMemberPath="Name" SelectedItem="{Binding SelectedCar, Mode=TwoWay}" />

But I'm not getting the two values "Audi" and "BMW" inside the combo box. Please tell if I've done something wrong.

Giggity
  • 54
  • 1
  • 7
  • 2
    Dont know about the answer but im not sure you can provide fields within an interface (C# 5?) so the best practice first is to have an Abstract class called Car. – Ori Refael Jul 31 '16 at 08:30
  • Sidenote: use also an interface for Content – Jeroen Heier Jul 31 '16 at 08:31
  • @OriRefael C# interfaces do allow properties to be defined. Ref: http://stackoverflow.com/questions/3754274/interface-class-with-getter-and-setter-in-c-sharp – Giggity Jul 31 '16 at 08:47
  • @Giggity for my opinion its the wrong way to use interfaces...especially in this case. – Ori Refael Jul 31 '16 at 08:48
  • @OriRefael Sir, why you think it's wrong? Can you please explain it to me – Giggity Jul 31 '16 at 08:53
  • What values are you getting in your dropdown? – DavidG Jul 31 '16 at 09:21
  • Name is not a public property ( get ). SelectedCar is not a not a public property. What you trying to do is just strange. You will never have two BMW? – paparazzo Jul 31 '16 at 11:48
  • Your code is fine. It shows Audi, BMW in the list. Make sure you are passing something in the ctor. – AnjumSKhan Aug 01 '16 at 05:54
  • @DavidG I'm not getting any value. Once I debugged it shows `IList Cars {get{return this.cars;}}` this.cars null – Giggity Aug 01 '16 at 14:18
  • @AnjumSKhan Is it getting the" Name" from the two classes? What did you do in the main? I tried to call but the default constructor which has no args is executing and I tried to chain the two constructors by using this(); in the constructor with args. But it says method name expected. Please tell me how you got it to work. – Giggity Aug 01 '16 at 14:32
  • Pass properly initialized list into ctor. – AnjumSKhan Aug 01 '16 at 14:40

2 Answers2

1

To get a list of classes which implement an interface:

  • Determine which assembly you're going to look in for types that implement ICar
  • Get all of the types in that assembly
  • Determine which of those types implement ICar.

Here's an example. For demonstration I'm looking for classes in the same assembly as the interface (ICar).

var assembly = Assembly.GetAssembly(typeof (ICar));
var typesInAssembly = assembly.GetTypes();
var matching = typesInAssembly
    .Where(type => typeof(ICar).IsAssignableFrom(type) && !type.IsInterface);
// Checking !type.IsInterface or else it also returns ICar.
Scott Hannen
  • 27,588
  • 3
  • 45
  • 62
0

please use observableCollection instead of List.

Class MainModel{
    List<ICar> cars;
    ICar selectedCar;

    ShowData(IEnumerable<ICar> cars){
        foreach (var car in cars)
            {
                _car.Add(car);
            }
        this.selectedCar = this.cars.FirstOrDefault();
    } 

     private ObservableCollection<Car> _cars=new ObservableCollection<Car>();

        public ObservableCollection<Car> Cars
        {
            get { return _cars; }
            set { _cars= value; }
        }
    public object Car{
        get{ return this.selectedCar.Content;}
    }
}
suulisin
  • 1,414
  • 1
  • 10
  • 17