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

编程入门 行业动态 更新时间:2024-10-28 03:24:19
'System.Collections.ObjectModel.ObservableCollection的最佳重载方法匹配有一些无效的参数(The best overloaded method match for 'System.Collections.ObjectModel.ObservableCollection has some invalid arguments)

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

Can some one please tell me Why is this not possible? I am new to WPF and Linq I am trying to select a value from my first combobox and display the related values in my second combobox.

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

最满意答案

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

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();

It looks like transTypeList is a List<string> (assuming transName.Name is a string) and you are trying to use it to initialise an ObservableCollection<TransactionTypeViewModel>.

The constructor for ObservableCollection<T> requires a List<T> so you need to provide a List<TransactionTypeViewModel> instead.

It looks like you just need to change your linq query to:

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

or alternatively:

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

更多推荐

本文发布于:2023-07-19 20:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1186683.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参数   方法   ObservableCollection   ObjectModel   Collections

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!