[英]Wpf, how to update/refresh ui after adding data to database
How can I make it so that whenever I add/update/remove data to database the UI is updated(in wpf). 如何做到这一点,以便每当我向数据库添加/更新/删除数据时,UI都会更新(在wpf中)。 I am using entity framework + local sql.
我正在使用实体框架+本地sql。 I have the following classes generated by entityframework : When I add the a transaction : I need/can specify a Payee and a TransactionCategory objects.
我有下列由EntityFramework生成的类:添加事务时:我需要/可以指定一个收款人和一个TransactionCategory对象。 public partial class Transaction { public int TransactionId { get;
公共部分类Transaction {public int TransactionId {get; set;
组; }
}
[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; }
}
And in their viewmodels the method I use to add data: 在他们的视图模型中,我使用了添加数据的方法:
The PayeeViewModel class : and the method to add. 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();
}
}
}
}
The TransactionCategoryViewModel update method is identical except the fields. 除了字段之外,TransactionCategoryViewModel更新方法相同。
In my TransactionViewModel : 在我的TransactionViewModel中:
I have a Payees and a Categories list : 我有一个收款人和一个类别列表:
The Payees list : 收款人清单:
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");
}
}
The Categories List 类别列表
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");
}
}
and the constructor of the TransactionViewModel 和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();
}
When I add a payee or transactioncategory to the database I'd like it to be reflected in all the views that contain a payee or transactioncategory. 当我将收款人或交易类别添加到数据库时,我希望它能反映在包含收款人或交易类别的所有视图中。 For example in the TransactionView(used to add/edit transactions) I have the fields mentioned in the TransactionClass and two combo boxes:one is binded to the Payees list and one to the CategoriesList.
例如,在TransactionView(用于添加/编辑交易)中,我在TransactionClass中有提到的字段和两个组合框:一个绑定到“收款人”列表,一个绑定到CategoriesList。
Problem is if I add/remove or update a Payee/TransactionCategory the comboboxes in the TransactionView are not updated accordingly. 问题是,如果我添加/删除或更新了Payee / TransactionCategory,那么TransactionView中的组合框不会相应地更新。
The TransactionView comboboxes: 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" />
Answering so it doesn't stay open. 回答所以它不会保持打开状态。 I created an ObservableCollection at startup which I added as a resource then declared local instances where I needed them.
我在启动时创建了一个ObservableCollection,将其添加为资源,然后在需要它们的地方声明了本地实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.