我正在使用通过以下代码combobox
将数据添加到datagrid
中。我要实现的是访问与所选项目的ID相关的所有“ item ”属性/信息,然后将所有信息设置为一个类(ExtraDisplayItems)。我将如何去做呢?
var item = cmbAddExtras.SelectedItem;
if (item != null)
dgAddExtras.Items.Add(item);
这是我的课:
public class ExtraDisplayItems
{
public int ItemId { get; set; }
public string ItemCode { get; set; }
public string ItemDescription { get; set; }
}
我已经开始使用数据绑定了,是在数据网格中显示项目的每个“ id”,而不再显示任何其他内容。
编辑:我正在尝试使用与我的组合框相同的类型,但是我遇到了一些麻烦。
如果有帮助,我只是在提供其他信息。在这里,我可以从WCF服务中获取所有信息,然后将其设置为WPF应用程序中的DisplayItems类:
private async Task LoadItems(TruckServiceClient TSC, QuoteOptionType type, ComboBox combobox)
{
List<DisplayItems> displayItems = new List<DisplayItems>();
foreach (var item in await TSC.GetQuoteOptionListAsync(type))
displayItems.Add(new DisplayItems { Id = item.Key, Name = item.Value });
combobox.ItemsSource = (displayItems.ToArray());
}
然后在下面的方法中,将数据从我的LoadItems方法加载到特定的组合框中:
private async void QuoteWindow_Loaded(object sender, RoutedEventArgs e)
{
using (TruckServiceClient TSC = new TruckServiceClient())
{
await LoadItems(TSC, QuoteOptionType.BodyType, cmbBodyType);
...
await LoadItems(TSC, QuoteOptionType.RearDropSide, cmbRearDropsideHeight);
await LoadItems(TSC, QuoteOptionType.Extras, cmbAddExtras); //Extras
}
//cmbAddExtras.SelectionChanged += cmbAddExtras_SelectionChanged;
}
在我的最后一个代码片段中,我试图将设置cmbAddExtras.SelectedItem
为我的Type(我认为是:)QuoteOptionType.Extras
。我正在尝试将SelectedItem设置如下,但不知道如何进行:(
var item = cmbAddExtras.SelectedItem as await QuoteOptionType.Extras;
或者
var item = cmbAddExtras.SelectedItem as await LoadItems(TSC, QuoteOptionType.Extras, cmbAddExtras);
我得到了错误:
'await'不能用作异步方法或lamba表达式中的标识符&
; 预期的
因为不等待此调用,所以在调用变得复杂之前,将继续执行当前方法。考虑将“ await”运算符应用于调用结果。
如您所见,我不知道自己在做什么。这也是我创建我的QuoteOptionType
班级/枚举的班级。这是从我的WCF应用程序中获得的:
[DataContract]
[Flags]
public enum QuoteOptionType
{
[EnumMember]
BodyType,
[EnumMember]
Chassis,
[EnumMember]
PaintColor,
[EnumMember]
DropSide,
[EnumMember]
Floor,
[EnumMember]
RearDropSide,
[EnumMember]
Extras
}
第2编辑:在这里我正在使用字典
public Dictionary<int, string> GetQuoteOptionList(QuoteOptionType optionType)
{
Dictionary<int, string> result = new Dictionary<int, string>();
using (TruckDb db = new TruckDb())
{
switch (optionType)
{
case QuoteOptionType.BodyType:
db.BodyTypes.ToList().ForEach(x => result.Add(x.Id, x.Name));
break;
...
case QuoteOptionType.RearDropSide:
db.RearDropSides.ToList().ForEach(x => result.Add(x.Id, x.Name));
break;
case QuoteOptionType.Extras: // x.StockItem throws out the error: No overload for method 'Add' takes 3 arguments
db.Extras.ToList().ForEach(x => result.Add(x.Id, x.Description, x.StockItem));
break;
default:
throw new ArgumentException("The option that was selected does not have a corresponding list.");
}
}
return result;
}
您要做的是将SelectedItem
转换为适当的类型。例如:
var item = cmbAddExtras.SelectedItem as DisplayItems;
if (item != null)
{
var displayItem = new ExtraDisplayItems();
displayItem.ItemId = item.Id;
displayItem.ItemCode = GetCode(item);
displayItem.ItemDescription = item.Name;
DoStuffWithYourDisplayItem(displayItem);
dgAddExtras.Items.Add(item);
}
即使SelectedItem
返回一个object
,在内部该对象实际上就是您的类(DisplayItems
),因此您可以强制转换并正常使用它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句