我使用combobox
将数据添加到datagrid
,其编码如下。我想要实现的是访问与所选项目的ID相关联的所有“项目”属性/信息,然后将所有这些信息设置为类( 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
设置为 类型 (我认为是QuoteOptionType.Extras
)。
我试图将SelectedItem设置如下,但不知道如何做到:(
var item = cmbAddExtras.SelectedItem as await QuoteOptionType.Extras;
或
var item = cmbAddExtras.SelectedItem as await LoadItems(TSC, QuoteOptionType.Extras, cmbAddExtras);
我收到了错误:
'await'不能用作异步方法中的标识符或 兰巴表达 &安培;
预期
由于不等待此调用,因此在调用复杂之前,将继续执行当前方法。考虑将'await'运算符应用于调用的结果。
你可以清楚地看到我有 NO 线索我在做什么。这也是我的课程,我在其中创建我的QuoteOptionType
类/枚举。这来自我的WCF应用程序:
[DataContract]
[Flags]
public enum QuoteOptionType
{
[EnumMember]
BodyType,
[EnumMember]
Chassis,
[EnumMember]
PaintColor,
[EnumMember]
DropSide,
[EnumMember]
Floor,
[EnumMember]
RearDropSide,
[EnumMember]
Extras
}
第二次编辑:我在这里使用词典
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;
}
答案 0 :(得分:1)
您需要做的是将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
),因此您可以将其强制转换并正常使用。