ArgumentException:此对象没有Name属性

时间:2015-11-19 09:40:59

标签: c# .net wpf wcf

我遇到了这个错误:

  

此对象没有Name属性,请使用另一个   构造器。

我从WCF调用数据来填充listview。我不完全理解错误或它为什么会发生?

WPF

public async Task LoadTrucks()
{
    TruckServiceClient TSC = new TruckServiceClient();
    try
    {
        var trucks = await TSC.GetTrucksAsync();
        foreach (var truck in trucks)
        lbTrucks.Items.Add(new ListBoxViewItem<RTrucks>(truck));
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

WCF

public List<RTrucks> GetTrucks()
{
    List<RTrucks> r = new List<RTrucks>();
    List<RTrucks> e;
    using (TruckDb db = new TruckDb())
    {
        e = db.RTrucks.Where(x => x.Id != null).ToList();
        foreach (var a in e)
        {
            var truck = new RTrucks()
            {
                Id = a.Id,
                ChassisManufacturer = a.ChassisManufacturer,
                ChassisModel = a.ChassisModel,
                PhaseId = a.PhaseId,
                PhaseStatusId = a.PhaseStatusId,
                QuoteId = a.QuoteId
            };
            r.Add(truck);
        }
        return r;
    }
}

1 个答案:

答案 0 :(得分:0)

列表视图会抛出此异常,因为您尝试将以下类的实例添加到列表视图中,并且该类没有Name属性

ListBoxViewItem<RTrucks> 

请将以下属性添加到ListBoxViewItem类,并使用传入的卡车实例的Id或其他属性在构造函数中设置它的值:

public class ListBoxViewItem<T>
{
    public string Name { get; set; }

    public ListBoxViewItem(T tInstance)
    {
        .... // current implementation
        Name = <set the name using tInstance>;
    }
}