2014-11-05 34 views
0

这是一个跟进到这样一个问题:计算DataGrid列不更新,源是一个实体

Entity Datagrid : best way to add computed values?

总之我想在数据网格的列显示其他两列中的差格。我希望计算列的值更新,因为我在其他列中输入值。

数据的来源是自动生成的实体。所以通过ADO.NET实体模型的过程去访问数据库的顶层类后:

public partial class BenchMarkEntities : DbContext 
    { 
     public BenchMarkEntities() 
      : base("name=BenchMarkEntities") 
     {   } 

     protected override void OnModelCreating(DbModelBuilder modelBuilder) 
     { 
      throw new UnintentionalCodeFirstException(); 
     } 

     public virtual DbSet<BenchmarkDescription> BenchmarkDescriptions { get; set; } 
     public virtual DbSet<SecurityDescription> SecurityDescriptions { get; set; } 
     public virtual DbSet<Weight> Weights { get; set; } 

这里是包含在DbSet集合中的示例类:

using System; 
using System.Collections.ObjectModel; 

public partial class Weight 
{ 
    public int BenchmarkID { get; set; } 
    public string Symbol { get; set; } 
    public decimal Benchmark_Weight { get; set; } 
    public decimal Security_Weight { get; set; } 
    public string Symbol_and_BenchmarkID { get; set; } 
    public bool Weight_Exists { get; set; } 

    public virtual BenchmarkDescription BenchmarkDescription { get; set; } 
    public virtual SecurityDescription SecurityDescription { get; set; } 
} 

在主窗口我有实例化BenchmarkEntities以便能够在数据库上执行CRUD操作。下面的代码来自教程。如果你有更好的方法会很乐意看到它。

public partial class MainWindow : Window 
{ 
    private BenchMarkEntities _context = new BenchMarkEntities(); 

    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    private void Window_Loaded(object sender, RoutedEventArgs e) 
    { 
     System.Windows.Data.CollectionViewSource weightViewSource = 
      ((System.Windows.Data.CollectionViewSource)(this.FindResource("weightViewSource"))); 

     _context.Weights.Load(); 
     // Load data by setting the CollectionViewSource.Source property: 
     weightViewSource.Source = _context.Weights.Local; 

    } 

因此,如另一个问题建议我添加了一个属性(在这种情况下,重量)。当我运行它时,我得到一个正确的初始值。但计算列不会更新,因为我在其他列中输入值。

public partial class Weight : INotifyPropertyChanged 
{ 
    public Weight() 
    { 
     this.PropertyChanged += ActiveWeightPropertyChanged; 
    } 

    void ActiveWeightPropertyChanged(object sender, PropertyChangedEventArgs e) 
    { 
     switch (e.PropertyName) 
     { 
      case "Benchmark_Weight": 
      case "Security_Weight": 
       OnPropertyChanged("DiffValues"); 
       break; 
     } 
    } 

    public decimal DiffValues 
    { 
     get 
     { 
      return Benchmark_Weight - Security_Weight; 
     } 
    } 

    #region INotifyPropertyChanged Members 

    public event PropertyChangedEventHandler PropertyChanged; 

    void OnPropertyChanged(string propName) 
    { 
     if (this.PropertyChanged != null) 
      this.PropertyChanged(
       this, new PropertyChangedEventArgs(propName)); 
    } 

    #endregion 

} 

}

这是我的XAML和第二DataGridTextColumn结合我加入addtional财产。

<DataGrid x:Name="weightDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" Margin="51,21,54,99" 
        ItemsSource="{Binding}" EnableRowVirtualization="True" AutoGenerateColumns="False"> 
      <DataGrid.Columns> 
       <DataGridTextColumn x:Name="benchmark_WeightColumn" Width="SizeToHeader" 
            Header="Benchmark Weight" Binding="{Binding Benchmark_Weight}"/> 
       <DataGridTextColumn x:Name="ActiveWeightColumn" Width="SizeToHeader" Header="ActiveWeight" 
            Binding="{Binding DiffValues}"/> 
       <DataGridTextColumn x:Name="security_WeightColumn" Width="SizeToHeader" Header="Security Weight" Binding="{Binding Security_Weight}"/> 
       <DataGridTextColumn x:Name="symbolColumn" Width="SizeToHeader" Header="Symbol" Binding="{Binding Symbol}"/> 
       <DataGridCheckBoxColumn x:Name="Weigh_ExistsColumn" Header="Remove" IsThreeState="False" Binding="{Binding Weight_Exists}"/> 
      </DataGrid.Columns> 
     </DataGrid> 

我想我知道该怎么做,但不知道它的可能。

  1. 我需要添加和实现INotifyPropertyChanged到我相信 的BenchMarkEntities类?

  2. 然后,我需要更改两个属性的设置器,这两个属性将通知DiffValues 。问题是这些是自动生成的。

或者是我需要将PropertyChanged事件及其方法添加到MainWindow?

任何帮助或建议?

谢谢

+0

请您添加作为网格数据源的类的代码?它是一个可观察的集合吗? – rodrigogq 2014-11-05 14:41:29

回答

0

确保你跟随这名:

public partial class MyEntity : INotifyPropertyChanged 
    { 
     // Methods 
    } 

    public class MyViewModel : INotifyPropertyChanged 
    { 
     private ObservableCollection<MyEntity> _listEntities = new ObservableCollection<MyEntity>(); 
     public ObservableCollection<MyEntity> ListEntities 
     { 
      get { return this._listEntities; } 
      set { /* code here... */} 
     } 
    } 

在这种情况下,您会在网格绑定到你的MyViewModel实例ListEntities。另外,尝试将绑定模式设置为TwoWay

0

需要raisepropertychanged的两个属性会通知DiffValues,是这样的:

public partial class Weight: INotifyPropertyChanged 
{ 
    //..... 
    decimal _benchmarkweight; 
    decimal _securityweight; 
    public decimal Benchmark_Weight 
    { 
    get{return _benchmarkweight;} 
    set{ 
     _benchmarkweight=value; 
     OnPropertyChanged("Benchmark_Weight "); 
     // OnPropertyChanged("DiffValues"); //don't need if you raise it already in this.PropertyChanged 
     } 
    } 
    public decimal Security_Weight 
    { 
    get{return _securityweight;} 
    set{ 
     _securityweight=value; 
     OnPropertyChanged("Security_Weight"); 
     // OnPropertyChanged("DiffValues"); //don't need if you raise it already in this.PropertyChanged 
     } 
    } 

    //..... 
} 

编辑:对不起没有意识到这是实体:

Based on the discussion here您需要更改WriteProperty方法在下面的templetes,以便它将生成属性与OnPropertyChanged

void WriteProperty(string accessibility, string type, string name, string getterAccessibility, string setterAccessibility) 
{ 
#> 
    private <#=type#> _<#=name#>; 
    <#=accessibility#> <#=type#> <#=name#> 
    { 
     <#=getterAccessibility#>get 
     { 
      return _<#=name#>; 
     } 
     <#=setterAccessibility#>set 
     { 
      if (value != _<#=name#>) 
      { 
       _<#=name#> = value; 
       OnPropertyChanged("<#=name#>"); 
      } 
     } 
    } 

<#+ 
} 
+0

感谢您的回复,我按照上面的建议尝试了一下,发现编译器错误。 Benchmark_Weight和Security_Weight已经在自动生成的文件中声明(第二个代码示例)。正如我在我的问题的底部提到的,我不认为我可以更改这些自动生成的属性。 – user1181337 2014-11-05 16:02:03

+0

@ user1181337,更新了我的答案。 – Bolu 2014-11-05 16:18:22

+0

Thanks @Bolu我会试试这个。我刚刚发现了一个类似(很好)的答案,它也更新了模板文件。 [链接] http://stackoverflow.com/questions/11010718/how-to-get-property-change-notifications-with-ef-4-x-dbcontext-generator/12192358#12192358 – user1181337 2014-11-05 16:23:21