开发者

WPF MVVM Combobox based on Entity Framework values does not raise property changed notification

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 开发者_如何学JAVAMonuments 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!!


According to this site that may be a bug.

What notification do you expect? All the binding does is that it changes the Monument property, if the setter of that property does not raise any notifications you will not get any notifications.


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:

public class Photo
    {
        [Key]
        public int PhotoId { get; set; }
        public int? MonumentId { get; set; }
        public int? MDPremisesSpaceId { get; set; }
        public int? MDRestaurationId { get; set; }
        public int? MDMaintenanceId { get; set; }
        public int? MDTypeId { get; set; }
        public string Year { get; set; }
        public byte[] Thumbnail { get; set; }
        public int? InsertUserId { get; set; }        //the Id of user who inserted this record
        public DateTime InsertTime { get; set; }     //insertion time
        public int? UpdateUserId { get; set; }        //the Id of last user who modified this record
        public DateTime UpdateTime { get; set; }     //last modification time



        //navigational properties
        public virtual Image Image { get; set; }
        [ForeignKey("MonumentId")]
        public virtual Monument Monument { get; set; }//a Photo belongs to a specific Monument
        [ForeignKey("MDPremisesSpaceId")]
        public virtual MDPremisesSpace MDPremisesSpace { get; set; } //a Photo was shot at specific premises space
        [ForeignKey("MDRestaurationId")]
        public virtual MDRestauration MDRestauration { get; set; }//a Photo has a restauration
        [ForeignKey("MDMaintenanceId")]
        public virtual MDMaintenance MDMaintenance { get; set; }//a Photo has a maintenance
        [ForeignKey("MDTypeId")]
        public virtual MDType MDType { get; set; }//a Photo has a type
        [ForeignKey("InsertUserId")]
        public virtual User InsertUser { get; set; }    //the user who inserted this reocord
        [ForeignKey("UpdateUserId")]
        public virtual User UpdateUser { get; set; }   //the user who lastly modified this reocord

        //constructor
        public Photo()
        {
        }
    }

Notice that every Foreign Key association has ALSO its property in the class. Now the property change notification takes place!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜