我有一个页面,上面有许多字段,用于创建一个名为 Account 的新课程记录。其中一个字段是通过组合框设置的货币代码。组合框绑定到具有 id 和描述的数据表。我正在尝试使用绑定,以便组合框的 selectedvalue 自动更新 Account 类的货币 id。到现在还没有喜...
类定义:
class Account : IDataErrorInfo
{
public String Name { get; set; }
public int CurrencyID { get; set; }
public int BankID { get; set; }
public String AccountNumber { get; set; }
public decimal OpeningBalance { get; set; }
... other definitions for validation handling ...
}
组合框定义:
<ComboBox x:Name="cboCurrency" Grid.Column="1" Grid.Row="1" Width="250" HorizontalAlignment="Left"
SelectedValue="{Binding Path=Account.CurrencyID, UpdateSourceTrigger=LostFocus, ValidatesOnDataErrors=True, NotifyOnValidationError=true}"
ToolTip="{Binding ElementName=cboCurrency, Path=(Validation.Errors)[0].ErrorContent}"/>
页面构造器:
public AccountAdd()
{
InitializeComponent();
base.DataContext = new Account();
// Load the Currency combo with the list of currencies
//
cboCurrency.DisplayMemberPath = "Name";
cboCurrency.SelectedValuePath = "_id";
cboCurrency.ItemsSource = _DBUtils.getCurrencyList().DefaultView;
}
保存代码:
private void btnAccountOK_Click(object sender, RoutedEventArgs e)
{
Account newAccountRec = (Account)base.DataContext;
int newid = _DBUtils.AddAccount(newAccountRec);
}