0

有人可以告诉我为什么这是不可能的吗?我是 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;
    }
}
4

1 回答 1

0

它看起来像transTypeList一个List<string>(假设transName.Name是一个字符串),你正试图用它来初始化一个ObservableCollection<TransactionTypeViewModel>.

for的构造函数ObservableCollection<T>需要 a List<T>,因此您需要提供 a 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();
于 2013-05-25T16:11:32.203 回答