0

使用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更新,當更改主人的信用百分比時,細節上的信用不會更新。

我沒有得到它可能有東西缺少讓兩個工作?

回答

0

不知道C#已經夠好了,但下面是我在VB.NET中實現的INotifyPropertyChanged。我不檢查處理程序狀態,但只是提高事件。從來沒有讓我失望過。

Public Event PropertyChanged As PropertyChangedEventHandler _ 
    Implements INotifyPropertyChanged.PropertyChanged 

Protected Sub OnPropertyChanged(ByVal info As String) 
    RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info)) 
End Sub 
+0

嘿tank @CodeWarrior需要時間,但我的問題更關係到這個問題:[link] http://stackoverflow.com/questions/2790054/change-notification-in-ef-entitycollection礦只是WPF的具體並且不使用RIA服務 – Worshound

+0

那麼您的實體是EF生成的默認實體嗎?如果是這樣,您不必實施INotifyPropertyChanged,因爲EF生成的實體(Not POCO)通過從實體基類繼承來實現該實例。此外,再次,我不是很好的C#,但我認爲你的PropertyChanged調用應該更像RaisePropertyChanged(「DepartmentLookup」); – CodeWarrior

0

展望您上傳該問題的代碼似乎是,你在後面的代碼(CourceViewSource_CurrentChanged),其中規定,當CreditPersentage在DataGrid改變時Credits財產,但沒有對方有一個事件處理程序 - 當Credits文本框更改時設置CreditPersentage的事件處理程序。

如果您與CreditsCreditPersentage之間的關係始終是100的因子,那麼在兩個字段中都有兩個數據庫列沒有多大意義。我將其中一個領域視爲「計算」和「依賴」,並在模型中直接表達這一點。在數據庫中,我只會存儲其中一個值。它看起來是這樣的:

public class Course : Notify 
{ 
    //... 

    private double _credits; // only one backing field for both properties 
    public double Credits 
    { 
     get { return _credits; } 
     set 
     { 
      _credits = value; 
      RaisePropertyChanged(() => Credits); 
      RaisePropertyChanged(() => CreditPersentage); 
      // notify WPF that also CreditPersentage has changed now 
     } 
    } 

    [NotMapped] // add using System.ComponentModel.DataAnnotations; for this 
    public double CreditPersentage 
    { 
     get { return _credits/100; } 
     set 
     { 
      _credits = value * 100; 
      RaisePropertyChanged(() => Credits); 
      // notify WPF that also Credit has changed now 
      RaisePropertyChanged(() => CreditPersentage); 
     } 
    } 

    public int DepartmentID { get; set; } 

    // also add change notification to Department 
    private Department _department; 
    public virtual Department Department 
    { 
     get { return _department; } 
     set 
     { 
      _department = value; 
      RaisePropertyChanged(() => Department); 
     } 
    } 
} 

[NotMapped]屬性可以確保有數據庫中沒有CreditPersentage列。該屬性僅基於_credits字段在內存中計算。

然後你可以在後面的代碼中刪除CourceViewSource_CurrentChanged。併爲您的文本框,你可以添加一個UpdateSourceTrigger

<TextBox Grid.Row="4" Grid.Column="1" 
     Text="{Binding Path=CourceViewSource/Credits, 
       UpdateSourceTrigger=PropertyChanged}" /> 

現在應該可以編輯在DataGrid在文本框中(百分比(當你寫的文本框被更新)和積分值的百分比列DataGrid在您寫入時得到更新)。當你在comboxbox中選擇一個新條目時,DataGrid中的Department列會被更新。