我想填充combobox items
与list of objects
,我从linq to sql
检索以下方式,(它只适用于我的select new
)。Linq到SQL的组合框绑定
请注意Tax
参数与选择新参数完全相同。
dt = new LilkaDataContext();
var taxes = from p in dt.Taxes select new
{
Id = p.Id,
Name = p.Name,
Percentage = p.Percentage,
Note = p.Note
};
cb.ItemsSource = taxes;
XAML
<ComboBox Name="CbSettingsTax" Grid.Column="2"
SelectedValue="Id"
DisplayMemberPath="Name" >
</ComboBox>
现在我需要解析的价值和检索中,我填充下拉框选择的税号。
int selectedTax = ((Tax)CbProductTax.SelectedItem).Id;
由于创建新对象select new
而得到以下错误。我想知道是否有任何方法来解析最后一步得到Tax.Id
或更好地填充没有select new
/创建新的对象。
其他信息:无法转换类型 '<> f__AnonymousType1
4[System.Int32,System.String,System.Nullable
1 [System.Decimal],System.String]' 的目的为类型 'lilka.Tax'。
编辑:
dt = new LilkaDataContext();
var taxes = from p in dt.Taxes select p;
cb.ItemsSource = taxes;
使用此查询我无法在GUI中选择组合框项目列表项。它只是不显示为选定项目。
感谢您的建议。我也尝试过使用这种方法,但我无法从GUI中的Combobox项目列表中选择项目。它只是不显示为选定项目。 – Marek 2015-02-07 00:15:11