我有一个填充了Income或Outflow对象的组合框。 收入和流出是从TransactionConcept继承的两个类。 他们有TPH EF Code First Inheritance。
public abstract class TransactionConcept
{
public int Id { get; set; }
public string Description { get; set; }
public virtual Status Status { get; set; }
public virtual User User { get; set; }
}
public class Income : TransactionConcept
{
public virtual IncomeType IncomeType { get; set; }
}
public class Outflow : TransactionConcept
{
public virtual OutflowType OutflowType { get; set; }
public virtual Category Category { get; set; }
public virtual PaymentType DefaultPaymentType { get; set; }
}
组合框以这种方式填充:
private void FillConcept()
{
IEnumerable<TransactionConcept> tConcepts;
cmbTransactionConcept.Items.Clear();
if (cmbTransactionType.SelectedIndex == 0)
{
var incomeRep = new IncomeRepository();
tConcepts = incomeRep.GetAll();
foreach (var inc in tConcepts)
{
cmbTransactionConcept.Items.Add(inc);
}
} else
{
var outflowRep = new OutflowRepository();
tConcepts = outflowRep.GetAll();
foreach (var outf in tConcepts)
{
cmbTransactionConcept.Items.Add(outf);
}
}
}
在设计器中,“Description”属性被选为DisplayMember,“Id”被选为ValueMember。
当我这样做时:
var a = (TransactionConcept)cmbTransactionConcept.SelectedItem;
仅提供属性ID和说明。
我想知道原因。
尝试将DisplayMember和ValueMember设置为none,它不会改变任何内容。 我认为它必须与虚拟属性有关。