【发布时间】:2015-07-21 13:16:20
【问题描述】:
我正在使用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;
}
在我的最终代码 sn-p 中,我试图将 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' 不能用作异步方法中的标识符或 兰巴语表达 &
;预计
由于不等待此调用,因此在调用复杂之前继续执行当前方法。考虑将“等待”运算符应用于调用结果。
你可以清楚地看到我不知道我在做什么。这也是我创建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;
}
【问题讨论】:
-
嘿,这段代码似曾相识;)
-
@almulo 哈哈嘿嘿!
-
好吧,让我们看看...首先,您的
SelectedItem总是返回您在ItemsSource中设置的相同类型的项目。由于您已将List<DisplayItems>设置为 ItemsSource,因此 SelectedItem 的类型必须为DisplayItems。 -
那么,在转换类型时不能使用
await...await用于执行对async方法的同步调用。您在QuoteWindow_Loaded中正确使用了它,但在投射SelectedItem时却没有。 -
最后,用于类型转换的
as关键字需要在其后添加一个 Type。LoadItems(...)不是类型,而是方法调用。而QuoteOptionType.Extras也不是类型,而是enum值(单独QuoteOptionType确实是一种类型......但在这种情况下不是正确的类型)