基于实体框架值的WPF MVVM组合框不会引发属性更改通知
我已经在以前的项目中成功地使用了MVVM原则,但我仍有一些问题。。。非常基本 使用EF4.1代码优先方法作为我的数据存储访问,效果很好。创建了以下类:基于实体框架值的WPF MVVM组合框不会引发属性更改通知,wpf,mvvm,entity-framework-4,inotifypropertychanged,Wpf,Mvvm,Entity Framework 4,Inotifypropertychanged,我已经在以前的项目中成功地使用了MVVM原则,但我仍有一些问题。。。非常基本 使用EF4.1代码优先方法作为我的数据存储访问,效果很好。创建了以下类: public class Photo { [Key] public int PhotoId { get; set; } public string Year { get; set; } public byte[] Thumbnail { get; set; }
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()
{
}
}
基于该类创建了一个Viemodel我正在使用一个基于泛型的Viewmodel,以在任何地方都可以继续使用它-成功地实现了INotifyPropertyChanged。在这个Viewmodel中还有一个名为Monuments的属性,它以这种方式定义很好:
private ObservableCollection<Model.Monument> monuments = new ObservableCollection<Model.Monument>();
public ObservableCollection<Model.Monument> Monuments
{
get { return monuments; }
private set { monuments = value; }
}
创建了一个视图,其中我使用了如下组合框:
<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"
/>
这些值显示正确,也可以正确保存
但是:
是否有人可以解释为什么更改组合框中的选定项不会引发属性更改通知
如果我要更改年份属性,通知将被提出
我怀疑上面照片类别中的纪念碑财产是导航财产……但即使是这样,它不应该发出通知吗
欢迎任何提示 根据这可能是一个bug
你希望得到什么样的通知?绑定所做的只是更改纪念碑属性,如果该属性的设置者不发出任何通知,您将不会收到任何通知。事实证明,我的怀疑是正确的 在EF4.1中声明此类代码第一类的正确方法如下:
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()
{
}
}
请注意,每个外键关联在类中也有其属性。现在属性更改通知发生了 那么,为什么它适用于我还没有定义的任何地方明确提出任何财产通知的年度财产?啊!!我明白了。现在,在阅读了错误投诉之后,它变得更有意义了。但我肯定我做错了什么…可能是我定义照片类的方式…我必须仔细研究!!