wpf - 将IEnumerable <T>与ObservableCollection <T>包装在一起

标签 wpf mvvm viewmodel ienumerable

从我的存储库中,我总是得到一个IEnumerable<T>

在Ctor或ViewModel的方法中,我想将我的实体包装成ObservableCollection<T>,如下所示:

private ObservableCollection<CustomerViewModel> customerViewModels;

public BillingViewModel(IService service)
{
   ...
   IEnumerable<Customer> customers = service.GetCustomers();

   // that will not work because of a different type
   customerViewModels = new ObservableCollection(customers); 

}

你会怎么做?

最佳答案

假设您有某种从Customer到CustomerViewModel的转换:

customerViewModels = new ObservableCollection<CustomerViewModel>
    (customers.Select(c => ConvertToViewModel(c)));

关于wpf - 将IEnumerable <T>与ObservableCollection <T>包装在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4280900/

相关文章:

android - 我可以管理生命周期所有者,用 Koin 注入(inject) viewModel 吗?

asp.net-mvc - 如何正确使用 View 模型

WPF - 为每个带有样式的 TreeViewItem 根节点设置不同的 ToggleButton 图像

wpf - 如何在 DrawingBrush 中重新映射颜色?

c# - 从ViewModel中选择 ListView 中的所有项目

c# - WPF ComboBox : Trouble converting back-and-forth of Selected Item (Works in all cases, 但就是这个。)

c# - WPF:DataGrid 上的水平滚动查看器在重新实例化绑定(bind)的 ObservableCollection 时捕捉到右侧

c# - 在 VS2010 中向后移植 .NET 4 到 3.5 SP1

android - 如何在 MVVM 架构的 RecyclerView 适配器中观察 LiveData?

c# - 无法隐式列出到 IEnumerable