这是一个跟进到这样一个问题:计算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>
我想我知道该怎么做,但不知道它的可能。
我需要添加和实现INotifyPropertyChanged到我相信 的BenchMarkEntities类?
然后,我需要更改两个属性的设置器,这两个属性将通知DiffValues 。问题是这些是自动生成的。
或者是我需要将PropertyChanged事件及其方法添加到MainWindow?
任何帮助或建议?
谢谢
请您添加作为网格数据源的类的代码?它是一个可观察的集合吗? – rodrigogq 2014-11-05 14:41:29