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.