'System.Collections.ObjectModel.ObservableCollection的最佳重载方法匹配有一些无效的参数

时间:2013-05-25 15:55:57

标签: wpf linq

有人可以告诉我为什么这不可能?我是WPF和Linq的新手 我试图从我的第一个组合框中选择一个值,并在我的第二个组合框中显示相关值。

private void initializeTransactionTypes()
{
    var getAppCode = applicationVModel.GetAllApplications().FirstOrDefault(apps =>   apps.AppCode == selectedApplication);

    var transTypeList = (from transName in transTypeVModel.GetAllTransactionTypes()
                         where transName.Id == getAppCode.Id
                         select transName.Name).ToList();


    //cast list of string to observ.
    ObservableCollection<TransactionTypeViewModel> transTypeObsList =
        new ObservableCollection<TransactionTypeViewModel>(transTypeList);

    TransactionTypes = transTypeObsList;

    NotifyPropertyChanged("TransactionTypes");
    // }

    //}
}

// Bind trans type combobox to this
public ObservableCollection<TransactionTypeViewModel> TransactionTypes
{
    set
    {
        initializeTransactionTypes();
        NotifyPropertyChanged("TransactionTypes");
    }
    get
    {
        return _transactionType;
    }
}

1 个答案:

答案 0 :(得分:0)

看起来transTypeListList<string>(假设transName.Name是字符串),并且您尝试使用它来初始化ObservableCollection<TransactionTypeViewModel>

constructor for ObservableCollection<T>需要List<T>,因此您需要提供List<TransactionTypeViewModel>

看起来您只需将linq查询更改为:

var transTypeList = (from transName in transTypeVModel.GetAllTransactionTypes()
                     where transName.Id == getAppCode.Id
                     select transName).ToList();

或者:

var transTypeList = transTypeVModel.GetAllTransactionTypes()
                                   .Where(t => t.Id == getAppCode.Id)
                                   .ToList();