I have a class :
public class Car
{
public string Name { get; set; }
public int Age { get; set; }
public Wheel[] Wheels {get;set;}
}
Collections of wheels can be changed
Every properties of Car will be showing at the same control
I want to see how make a ViewModel for this class.
Had I good understood conception of MVVM ?
public class CarViewModel()
{
ObservableCollection<Wheel> _wheels{ get; set; }
Car _car {get;set;}
public ObservableCollection<Wheel> Wheels
{
get{ return this._car.Wheels;}
set{ this._car.Wheels=value}
}
public string Name
{
get{ return this._car.Name;}
set{ this._car.Name=value}
}
public int Age
{
get{ return this._car.Age;}
set{ this._car.Age=value;}
}
public CarViewModel()
{
this._car=GetCar();
}
}
}
I paste code of my application where I have problem with this MVVM(I'm not sure of good implementation, And removing Facility doesn't work. Could you help me rebuild this code?
Part of UserControlHotelDescription.xaml
<ListBox Grid.Column="1" ItemsSource="{Binding Path=CheckedRoomFacilities}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding Path=IsChecked}" Click="CheckBox_Click"/>
<TextBlock Text="{Binding Path=Name}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
public partial class UserControlHotelDescription : UserControl
{
public UserControlHotelDescription()
{
InitializeComponent();
}
private void CheckBox_Click(object sender, RoutedEventArgs e)
{
CheckBox checkBox = sender as CheckBox;
CheckableFacility checkableFacility = checkBox.DataContext as CheckableFacility;
if (checkBox.IsChecked.GetValueOrDefault())
{
((HotelDescriptionModelView)this.DataContext).RoomFacilities.Add(new Facility() { Name = checkableFacility.Name,Id=checkableFacility.Id });
}
else
{
((HotelDescriptionModelView)this.DataContext).RoomFacilities.Remove(((HotelDescriptionModelView)this.DataContext).RoomFacilities.Where(ce => ce.Id == checkableFacility.Id).First());
}
}
}
public class HotelDescriptionModelView
{
public HotelDescription HotelDescription { get; set; }
List<Facility> AvailableFacilities { get; set; }
public HotelDescriptionModelView()
{
//there is Equal to: HotelDescription =GetHotelDescription()
HotelDescription = new HotelDescription();
HotelDescription.Name = "Hilton";
List<Facility> gotFacilities = new List<Facility>();
gotFacilities.Add(new Facility() { FacilityCategory = FacilityCategory.Relax, Id = 1, Name = "Jacuzzi", FacilityType = FacilityType.Room });
gotFacilities.Add(new Facility() { FacilityCategory = FacilityCategory.Sport, Id = 2, Name = "Trash", FacilityType = FacilityType.Room });
gotFacilities.Add(new Facility() { FacilityCategory = FacilityCategory.Sport, Id = 4, Name = "Chairlift", FacilityType = FacilityType.SkiSlope });
HotelDescription.Facilities = gotFacilities.ToArray();
AvailableFacilities = new List<Facility>();
AvailableFacilities.Add(new Facility() { FacilityCategory = FacilityCategory.Relax, Id = 1, Name = "Jacuzzi", FacilityType = FacilityType.Room });
AvailableFacilities.Add(new Facility() { FacilityCategory = FacilityCategory.Relax, Id = 2, Name = "Trash", FacilityType = FacilityType.Room });
AvailableFacilities.Add(new Facility() { FacilityCategory = FacilityCategory.Relax, Id = 3, Name = "AirCondition", FacilityType = FacilityType.Room });
AvailableFacilities.Add(new Facility() { FacilityCategory = FacilityCategory.Relax, Id = 4, Name = "Chairlift", FacilityType = FacilityType.SkiSlope });
AvailableFacilities.Add(new Facility() { FacilityCategory = FacilityCategory.Relax, Id = 5, Name = "T-bar lift", FacilityType = FacilityType.SkiSlope });
}
public string Name
{
get
{
return this.HotelDescription.Name;
}
set
{
this.HotelDescription.Name = value;
}
}
public List<Facility> RoomFacilities
{
get
{
return
this.HotelDescription.Facilities.Where(
f => f.FacilityType == FacilityType.Room).ToList();
}
set
{
this.RoomFacilities = value;
}
}
public CheckableFacility[] CheckedRoomFacilities
{
get
{
return (from fl in AvailableFacilities.Where(af => af.FacilityType == FacilityType.Room).Union(RoomFacilities)
group fl by fl.Name into d
select new CheckableFacility()
{
Name = d.Key,
IsChecked = (d.Count() > 1)
}).ToArray();
}
set
{
this.CheckedRoomFacilities = value;
}
}
}
public class CheckableFacility
{
public int Id { get; set; }
public FacilityCategory ReporterFacilityCategory { get; set; }
public bool IsChecked { get; set; }
public string Name { get; set; }
}