I already have succesfully used MVVM principles in former projects, but i’m stuck with something … very basic!
Using EF4.1 (code first approach) as my data storage access which works fine. Created the following class:
public class Photo
{
[Key]
public int PhotoId { get; set; }
public string Year { get; set; }
public byte[] Thumbnail { get; set; }
public DateTime InsertTime { get; set; } //insertion time
public DateTime UpdateTime { get; set; } //last modification time
//navigational properties
public virtual Image Image { get; set; }
public virtual Monument Monument { get; set; }//a Photo belongs to a specific Monument
public virtual MDPremisesSpace MDPremisesSpace { get; set; } //a Photo was shot at specific premises space
public virtual MDRestauration MDRestauration { get; set; }//a Photo has a restauration
public virtual MDMaintenance MDMaintenance { get; set; }//a Photo has a maintenance
public virtual MDType MDType{ get; set; }//a Photo has a type
public virtual User InsertUser { get; set; } //the user who inserted this reocord
public virtual User UpdateUser { get; set; } //the user who lastly modified this reocord
//constructor
public Photo()
{
}
}
Created a Viemodel based on that class (i ‘m using a Viewmodel based on generics, to resuse it whereever i can – with success), which implements INotifyPropertyChanged.In this viewmodel there is also a Property called Monuments which is defined this way (works fine):
private ObservableCollection<Model.Monument> monuments = new ObservableCollection<Model.Monument>();
public ObservableCollection<Model.Monument> Monuments
{
get { return monuments; }
private set { monuments = value; }
}
Created a view in which i used a combobox like this:
<ComboBox Name="cmbMonument" Grid.Row="1" Grid.Column="1" Width="200" HorizontalAlignment="Left"
ItemsSource="{Binding Path=Monuments }"
SelectedItem="{Binding Path=SelectedRecord.Monument,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
DisplayMemberPath="Name"
/>
The values are showing up correclty (and they can also be saved correctly).
BUT:
Is there anyone who can explain why changing the selected item in the combobox, it DOES NOT raise a property change notification??
If i would change the Year property the notification would get raised!!
(i’m suspecting the fact that the Monument property in the Photo class above is a navigational property…but even if it is like that, shouldn’t it raise a notification?)
Any hints are mostly welcome!!
Well it turns out that my suspicions where correct!
The correct way to declare this kind of code first class in EF4.1 is the following:
Notice that every Foreign Key association has ALSO its property in the class. Now the property change notification takes place!