【问题标题】:ArgumentException: This object does not have a Name propertyArgumentException:此对象没有 Name 属性
【发布时间】:2015-11-19 09:40:58
【问题描述】:

我遇到了这个错误:

此对象没有 Name 属性,请使用其他 构造函数。

我正在从 WCF 调用数据来填充列表视图。我不完全理解错误或为什么会发生?

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;
    }
}

【问题讨论】:

  • 但是你可以告诉我们它发生在哪里吗?
  • 是的,我在哪里添加项目.. lbTrucks.Items.Add(new ListBoxViewItem&lt;RTrucks&gt;(truck)); @oguzozgul
  • 您是否尝试按照错误提供的“使用其他构造函数”说明进行操作?
  • 不,我没有尝试过使用另一个,对不起,我对构造函数是什么有基本的了解。但是我不知道要使用什么其他构造函数。我在填充组合框时已经尝试过这种方法,但是我在列表框中收到了这个错误。 @MikeEason
  • ListBoxViewItem 是你的班级吗?如果是,请分享该课程的代码。如果是您的班级,请添加一个公共字符串 Name {get; set;} 属性并在构造函数中设置它

标签: c# .net wpf wcf


【解决方案1】:

列表视图抛出此异常是因为您尝试将以下类的实例添加到列表视图中,而该类没有 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>;
    }
}

【讨论】:

    猜你喜欢
    • 2020-05-10
    • 1970-01-01
    • 2021-08-05
    • 2017-04-15
    • 2021-10-22
    • 2021-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多