[英]WPF - ComboBox SelectedItem not binding
我有两种看法:一种是清单,另一种是表格来添加/编辑票据。
BillView包含一些文本框和一个可填充的组合框。问题是,如果我尝试在BillView打开时编辑帐单,则这些文本框将取代我在添加帐单时输入的值,但组合框不会显示我在添加时选择的值帐单(但列表绑定没问题)。 在BillAccount集合上使用了一个断点,它似乎可以工作,但是未设置comboxbox selecteditem。
BillView(用于添加/编辑帐单):
<Label Grid.Row="5" HorizontalAlignment="Left" Content="Account" />
<ComboBox Grid.Row="5" ItemsSource="{Binding Accounts}" DisplayMemberPath="TheAccount.AccountName" HorizontalAlignment="Right"
SelectedItem="{Binding BillAccount}"/>
这是BillViewModel的构造函数:
public BillViewModel(BillModel bill)
{
BillId = bill.TheBill.BillId;
AccountId = bill.TheBill.AccountId;
BillAmmount = (double)bill.TheBill.Ammount;
NextDate = bill.TheBill.NextDate;
this.accounts = GetAccounts(); // populates the list
billAccount = accounts.Where(i => i.TheAccount.AccountId == this.AccountId).First();
}
public AccountModel BillAccount
{
get { return billAccount; }
set { billAccount = value;
OnPropertyChanged("BillAccount");
}
}
GetAccounts()方法:
private ObservableCollection<AccountModel> GetAccounts()
{
if (accounts == null)
accounts = new ObservableCollection<AccountModel>();
accounts.Clear();
using(var context = new Ents())
{
foreach(var account in context.Accounts)
{
accounts.Add(new AccountModel() { TheAccount = account });
}
}
return accounts;
}
该方法应该很好。
账单账户是可变的而非财产-
billAccount = accounts.Where(i => i.TheAccount.AccountId == this.AccountId).First();
改成
BillAccount = accounts.Where(i => i.TheAccount.AccountId == this.AccountId).First();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.