我想填充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