【问题标题】:Accessing assigned variable items within combobox selected item访问组合框选定项中分配的变量项
【发布时间】:2015-07-21 13:16:20
【问题描述】:

我正在使用comboboxdatagrid 添加数据,代码如下。我想要实现的是访问连接到该选定项目的 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&lt;DisplayItems&gt; 设置为 ItemsSource,因此 SelectedItem 的类型必须为 DisplayItems
  • 那么,在转换类型时不能使用await...await 用于执行对async 方法的同步调用。您在 QuoteWindow_Loaded 中正确使用了它,但在投射 SelectedItem 时却没有。
  • 最后,用于类型转换的 as 关键字需要在其后添加一个 Type。 LoadItems(...) 不是类型,而是方法调用。而QuoteOptionType.Extras 也不是类型,而是enum 值(单独QuoteOptionType 确实是一种类型......但在这种情况下不是正确的类型)

标签: c# wpf datagrid combobox


【解决方案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),所以你可以转换它并正常使用它。

【讨论】:

  • 抱歉,almulo 回复晚了!我正在尝试按照您所说的去做,但是在我的组合框中的信息“类型”有很多问题。请参阅我在问题中发布的编辑。非常杂乱无章,但我不知道如何以任何其他方式问它。
  • 好的,我知道你在做什么,再次非常感谢你付出的所有努力和时间! :D 我要到某个地方了!现在唯一的事情是,在我的 WCF 应用程序的GetQuoteOptionList 方法中,我只能设置` (x => result.Add(x.Id, x.Description));` 而不能添加另一个“(x =&gt; result.Add(x.Id, x.Description, x.StockItem)) ”。我在 WCF 应用程序的 IService 中使用字典。我在我的回答中发布了第二次编辑:P
  • 我想这应该放在第三篇文章中,因为这是一个不同的问题哈哈 但问题是字典只存储两个值:键和值。如果您想存储更多信息,这两个(通常是值)之一应该是某种struct 类型,其中存储两个或多个值。所以它会像(x =&gt; result.Add(x.Id, new ExtraValues { Description = x.Description, StockItem = x.StockItem }))... 查找有关struct 的信息以及如何创建您的ExtraValues 一个;)
  • 好的,我会调查的。非常感谢almulo! :)
  • 嗨,almulo :) 我知道这是非常随机的,但我记得到目前为止你帮助我解决了一些单独的问题,而且我从你的个人资料中知道你擅长 WPF 和 UI。我迫切需要帮助,因为没有办法 PM 你我需要这样做。如果你有时间,你能看看我的这个问题Link。到目前为止,没有人评论或回答我的问题。我可能看起来很想这样做,但我真的很渴望帮助
猜你喜欢
  • 1970-01-01
  • 2023-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多