使用EF 4.1我添加了INotifyPropertyChanged接口,以在屬性更改時通知我的視圖。INotifyPropertyChanged實現不與實體框架一起使用4.1導航屬性
public class Department : INotifyPropertyChanged
{
public Department()
{
this.Courses = new ObservableCollection<Course>();
}
// Primary key
public int DepartmentID { get; set; }
private string _name;
public string Name
{
get { return _name; }
set
{
_name = value;
RaisePropertyChanged("Name");
}
}
// Navigation property
public virtual ObservableCollection<Course> Courses { get; private set; }
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public class Course : INotifyPropertyChanged...
在主詳細情況我有一個查找組合改變該部: 當INotifyPropertyChanged的實施部門屬性不會更新,但去除部的INotifyPropertyChanged的實施和課程類它的作用:
XAML
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<DataGrid
AutoGenerateColumns="False"
EnableRowVirtualization="True"
Height="173"
HorizontalAlignment="Left"
ItemsSource="{Binding CourceViewSource}"
x:Name="departmentDataGrid"
RowDetailsVisibilityMode="VisibleWhenSelected"
VerticalAlignment="Top"
Width="347">
<DataGrid.Columns>
<DataGridTextColumn x:Name="CourseID" Binding="{Binding Path=CourseID}"
Header="CourseID" Width="SizeToHeader" />
<DataGridTextColumn x:Name="nameColumn" Binding="{Binding Path=Title}"
Header="Title" Width="SizeToHeader" />
<DataGridTextColumn x:Name="nameColumnw" Binding="{Binding Path=Department.Name}"
Header="Department" Width="SizeToHeader" />
</DataGrid.Columns>
</DataGrid>
<ComboBox Grid.Row="1"
ItemsSource="{Binding DepartmentLookUp}"
SelectedItem="{Binding CourceViewSource/Department}" />
<Button Grid.Row="2" Content="Save" Click="Button_Click"/>
</Grid>
代碼隱藏 ...
private SchoolEntities _context = new SchoolEntities();
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
}
public ICollectionView CourceViewSource { get; private set; }
public ICollectionView DepartmentLookUp { get; private set; }
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
_context.Departments.Load();
_context.Courses.Load();
DepartmentLookUp = new ListCollectionView(_context.Departments.Local);
CourceViewSource= new ListCollectionView(_context.Courses.Local);
RaisePropertyChanged(() => DepartmentLookUp);
RaisePropertyChanged(() => CourceViewSource);
}
...
我已經包括了問題here的樣本。
在細節中選擇一個部門時,主人的部門不更新,當更改主人的信用百分比時,細節上的信用會更新。
現在改變SchoolModel.cs,以便通知類不實現INotifyPropertyChanged接口(公共類通知//:INotifyPropertyChanged的):
當細節選擇一處系在主DO更新,當更改主人的信用百分比時,細節上的信用不會更新。
我沒有得到它可能有東西缺少讓兩個工作?
嘿tank @CodeWarrior需要時間,但我的問題更關係到這個問題:[link] http://stackoverflow.com/questions/2790054/change-notification-in-ef-entitycollection礦只是WPF的具體並且不使用RIA服務 – Worshound
那麼您的實體是EF生成的默認實體嗎?如果是這樣,您不必實施INotifyPropertyChanged,因爲EF生成的實體(Not POCO)通過從實體基類繼承來實現該實例。此外,再次,我不是很好的C#,但我認爲你的PropertyChanged調用應該更像RaisePropertyChanged(「DepartmentLookup」); – CodeWarrior