【发布时间】:2015-02-09 20:08:54
【问题描述】:
我正在尝试使用从linq to sql 检索的list of objects 填充combobox items,方法如下(仅适用于select new)。
注意Tax参数和select new完全一样。
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.Nullable1[System.Decimal],System.String]”的对象转换为类型“lilka.Tax”。
编辑:
dt = new LilkaDataContext();
var taxes = from p in dt.Taxes select p;
cb.ItemsSource = taxes;
使用此查询,我无法从 GUI 的组合框项目列表中选择项目。它只是不显示为选定项目。
【问题讨论】:
标签: c# wpf linq linq-to-sql combobox