C# 如何在列表中插入项目

C# 如何在列表中插入项目,c#,wpf,list,datagrid,C#,Wpf,List,Datagrid,在下面的编码中,我从组合框将项目插入到数据网格 private void cmbAddExtras_SelectionChanged(object sender, SelectionChangedEventArgs e) { using (TruckServiceClient TSC = new TruckServiceClient()) { var item = cmbAddExtras.SelectedItem as ExtraDisplayItems;

在下面的编码中,我从组合框将项目插入到数据网格

private void cmbAddExtras_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    using (TruckServiceClient TSC = new TruckServiceClient())
    {
        var item = cmbAddExtras.SelectedItem as ExtraDisplayItems;

        if (item != null)
        {
            var displayItem = new List<ExtraDisplayItems>
            {
                new ExtraDisplayItems 
                {                            
                    ItemId = item.ItemId, 
                    ItemCode = item.ItemCode, 
                    ItemDescription = item.ItemDescription, 
                    ItemSellingPrice = item.ItemSellingPrice,
                    displayItems = item.displayItems //Always null?
                }
            };
            dgAddExtras.Items.Add(item);
        }
    }
    btnRemoveAllExtras.Visibility = Visibility.Visible;
}
private void cmbAddExtras\u SelectionChanged(对象发送方,SelectionChangedEventArgs e)
{
使用(TruckServiceClient TSC=new TruckServiceClient())
{
var item=cmbAddExtras.SelectedItem作为ExtraDisplayItems;
如果(项!=null)
{
var displayItem=新列表
{
新的ExtraDisplayItems
{                            
ItemId=item.ItemId,
ItemCode=item.ItemCode,
ItemDescription=item.ItemDescription,
ItemSellingPrice=item.ItemSellingPrice,
displayItems=item.displayItems//是否始终为空?
}
};
dgAddExtras.Items.Add(item);
}
}
btnRemoveAllExtras.Visibility=可见性.Visibility;
}
我在下面的类中创建了一个变量,我希望能够以不同的方法访问项目,并获得ItemSellingPrice的总和

我的班级:

public class ExtraDisplayItems
{
    public List<ExtraDisplayItems> displayItems;

    public int ItemId { get; set; }    
    public string ItemCode { get; set; }    
    public string ItemDescription { get; set; }    
    public double? ItemSellingPrice { get; set; }
}
公共类ExtraDisplayItems
{
公共物品清单;
公共int ItemId{get;set;}
公共字符串ItemCode{get;set;}
公共字符串ItemDescription{get;set;}
公共双项目SellingPrice{get;set;}
}

现在我的问题是,在我将项插入数据网格的顶部方法中,出于某种原因,我的
displayItems
变量总是null。是否有一些特殊的方式需要将项目加载到我的类中的
displaytems
列表中?

cmbAddExtras。SelectedItem
不是
extrasplaytems
类型。它要么为null,要么为其他类型

您不需要在添加到数据网格的每个项目上存储所选项目的整个集合。您可以从DataGrid本身检索集合,例如,您可以使用这样的计算属性(您可能需要将
System.Linq
添加到您的使用中):

您的
SelectionChanged
方法将以如下方式结束:

private void cmbAddExtras_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    // You're not using TSC, so you don't need this either
    //using (TruckServiceClient TSC = new TruckServiceClient())
    //{
        var item = cmbAddExtras.SelectedItem as ExtraDisplayItems;

        if (item != null)
        {
            dgAddExtras.Items.Add(item);
        }
    //}
    btnRemoveAllExtras.Visibility = Visibility.Visible;
}
在另一个需要计算
ItemSellingPrice
之和的方法中,您只需要使用计算属性

private void YourOtherMethod()
{
    // do stuff

    var sum = SelectedDisplayItems.Sum(item => item.ItemSellingPrice ?? 0); // Since ItemSellingPrice can be null, use 0 instead

    // do more stuff
}

您是否尝试过实例化displayItems?@Turbusion-谢谢您的回复!:)我该怎么做呢?请给我举个例子好吗?给你
公共ExtraDisplayItems(){this.displayItems=new List();}
谢谢!我应该将它插入我的方法还是其他地方?它是ExtraDisplayItems类的构造函数。使用它谢谢你的回答,格伦,但这仍然给我的
displayItems
一个空值。哈哈哈哇!!!我在这个问题上挣扎了很久,现在你给了我答案。它工作得很好!非常感谢阿尔穆洛!!:在我要问的所有问题中,我的答案是最好的。
private void cmbAddExtras_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    // You're not using TSC, so you don't need this either
    //using (TruckServiceClient TSC = new TruckServiceClient())
    //{
        var item = cmbAddExtras.SelectedItem as ExtraDisplayItems;

        if (item != null)
        {
            dgAddExtras.Items.Add(item);
        }
    //}
    btnRemoveAllExtras.Visibility = Visibility.Visible;
}
private void YourOtherMethod()
{
    // do stuff

    var sum = SelectedDisplayItems.Sum(item => item.ItemSellingPrice ?? 0); // Since ItemSellingPrice can be null, use 0 instead

    // do more stuff
}