我怎样才能做到这一点,每当我向数据库添加/更新/删除数据时,UI都会更新(在wpf中)。 我正在使用实体框架+本地sql。 我有entityframework生成的以下类: 当我添加一个事务时:我需要/可以指定一个Payee和一个TransactionCategory对象。 公共部分类交易 { public int TransactionId {get;组; }
[Column(TypeName = "money")]
public decimal TransactionAmmount { get; set; }
public int? TransactionCategoryId { get; set; }
public int? PayeeId { get; set; }
[Column(TypeName = "money")]
public decimal? TotalAmmount { get; set; }
public int? TransactionType { get; set; }
[Column(TypeName = "date")]
public DateTime? TransactionDate { get; set; }
public virtual Payee Payee { get; set; }
public virtual TransactionCategory TransactionCategory { get; set; }
}
public partial class Payee
{
public Payee()
{
Bills = new HashSet<Bill>();
Transactions = new HashSet<Transaction>();
}
public int PayeeId { get; set; }
[Required]
[StringLength(50)]
public string PayeeName { get; set; }
public bool HasAccounts { get; set; }
[StringLength(50)]
public string PayeePhone { get; set; }
[StringLength(50)]
public string PayeeAddress { get; set; }
public virtual ICollection<Bill> Bills { get; set; }
public virtual ICollection<Transaction> Transactions { get; set; }
}
public partial class TransactionCategory
{
public TransactionCategory()
{
Bills = new HashSet<Bill>();
Transactions = new HashSet<Transaction>();
}
[Key]
public int CategoryId { get; set; }
[Required]
[StringLength(50)]
public string CategoryName { get; set; }
[Column(TypeName = "money")]
public decimal CategoryExpenseLimit { get; set; }
[Column(TypeName = "money")]
public decimal CategoryExpense { get; set; }
[Column(TypeName = "money")]
public decimal CategoryIncome { get; set; }
[StringLength(50)]
public string Period { get; set; }
public virtual ICollection<Bill> Bills { get; set; }
public virtual ICollection<Transaction> Transactions { get; set; }
}
在他们的viewmodels中我使用的方法来添加数据:
PayeeViewModel类:以及要添加的方法。
private void Update()
{
using (var context = new Ents())
{
if (this.Mode == Mode.Add)
{
if (string.IsNullOrEmpty(PayeeName))
{
MessageBox.Show("A Payee Name must be entered");
}
else
{
PayeeModel payee = new PayeeModel() { ThePayee = { PayeeName = PayeeName, PayeeAddress = PayeeAddress, PayeePhone = PayeePhone, HasAccounts = PayeeHasAccount } };
context.Payees.Add(payee.ThePayee);
context.SaveChanges();
}
}
}
}
除了字段外,TransactionCategoryViewModel更新方法相同。
在我的TransactionViewModel中:
我有一个Payees和一个类别列表:
Payees列表:
private ObservableCollection<PayeeModel> payees;
private PayeeModel selectedPayee;
public ObservableCollection<PayeeModel> Payees
{
get { return payees; }
set
{
payees = value;
OnPropertyChanged("Payees");
}
}
public PayeeModel SelectedPayee
{
get { return selectedPayee; }
set { selectedPayee = value;
OnPropertyChanged("SelectedPayee");
}
}
类别列表
private ObservableCollection<CategoryModelModel> categories;
private CategoryModel selectedCategory;
public ObservableCollection<CategoryModel> Categories
{
get { return payees; }
set
{
payees = value;
OnPropertyChanged("Categories");
}
}
public CategoryModel SelectedCategory
{
get { return selectedCategory; }
set { selectedCategory = value;
OnPropertyChanged("SelectedCategory");
}
}
和TransactionViewModel的构造函数
internal TransactionViewModel(TransactionModel transaction, IEventAggregator eventAggregator)
{
this.eventAggregator = eventAggregator;
TransactionId = transaction.TheTransaction.TransactionId;
ammount = (double)transaction.TheTransaction.TransactionAmmount;
PayeeId = transaction.TheTransaction.PayeeId;
GetCategories();
GetPayees();
SelectedPayee = payees.Where(i => i.ThePayee.PayeeId == PayeeId).First();
SelectedCatgory = categories.Where(i => i.TheCategory.CategoryId == CategoryId).First();
}
当我向数据库添加收款人或交易类别时,我希望它反映在包含收款人或交易类别的所有视图中。 例如在TransactionView中(用于添加/编辑事务) 我有TransactionClass中提到的字段和两个组合框:一个绑定到Payees列表,一个绑定到CategoriesList。
问题是,如果我添加/删除或更新Payee / TransactionCategory,则TransactionView中的组合框不会相应更新。
TransactionView组合框:
<Label Content="Payee" Grid.Row="2" Margin="3" />
<ComboBox Grid.Column="1" Grid.Row="2" Margin="3" Width="100" ItemsSource="{Binding Payees}" SelectedItem="{Binding SelectedPayee}"
DisplayMemberPath="ThePayee.PayeeName"/>
<Label Content="Category" Grid.Row="3" Margin="3" />
<ComboBox Grid.Column="1" Grid.Row="3" Margin="3" Width="100" ItemsSource="{Binding Categories}"
SelectedItem="{Binding SelectedCategory}" DisplayMemberPath="TheCategory.CategoryName" />
答案 0 :(得分:0)
回答所以它不会保持开放。 我在启动时创建了一个ObservableCollection,我将其作为资源添加,然后在需要它们的地方声明了本地实例。