仅查看属性(例如:IsSelected)和MVVM中的Model [英] View only properties (eg: IsSelected) and the Model in MVVM

查看:179
本文介绍了仅查看属性(例如:IsSelected)和MVVM中的Model的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑使用MVVM编写的WPF应用程序。应用程序需要显示Employees列表(FirstName,LastName,Title),您可以选择多个删除。



在这种情况下,模型将是 Employee ,它将实现 INotifyPropertyChanged



该视图将是 EmployeeListView 将实现XAML来显示员工的集合。



ViewModel将是 EmployeeListViewModel ,这将暴露一个 ObservableCollection ,可以绑定到 EmployeeListView



我的问题是: IsSelected 应该在哪里财产现在?


  1. 在模型中(我不喜欢这个想法,因为模型现在正在暴露属性只需要一个视图,与实际的域对象无关,同样,这个属性将是无用的,如果我实现的视图不同,不允许

  2. EmployeeListViewModel 中,作为单独的字典集合,可以跟踪员工是否被选择? (或者甚至只包含所有选定的员工的HashSet)。我不喜欢这样,因为视图中的约束不再是直接的。

  3. 实现一个单独的EmployeeViewModel ,它包装Employee对象并公开IsSelected属性。然后EmployeeListViewModel将其集合公开为ObservableCollection。我喜欢这个解决方案是最好的,但我一直认为每个View有一个ViewModel,在这种情况下,我有两个视图模型供我看。 是否是偏离MVVM模式?这是实现MVVM的典型方式吗?


解决方案

创建一个可重用的Generic SelectableItem,将每个项目包含在EmployeeList中:



简单示例:

  public class SelectableItem< T> ;: INotifyPropertyChanged 
{
public bool IsSelected {get; set;} // PropertyChanged(),etc

public T Value {get; set;}
}

然后在ViewModel中:

  public ObservableCollection< SelectableItem&Employee>>员工{get; set;} 

和查看:

 < DataTemplate> 
< CheckBox IsChecked ={Binding IsSelected}Content ={Value.FullName}/>
< / DataTemplate>

然后,您可以通过以下方式检索所有选定的员工:

  var selectedemployees = Employees.Where(x => x.IsSelected).Select(x => x.Value); 


Consider a WPF app that is being written using MVVM. The app needs to display a list of Employees (FirstName, LastName, Title) and you can select multiple to delete.

The model in this case would be "Employee" and it would implement INotifyPropertyChanged.

The View would be the "EmployeeListView" which would implement the XAML to display a collection of Employees.

The ViewModel would be "EmployeeListViewModel" which would expose an ObservableCollection, that can be bound to the EmployeeListView.

My question is: Where should the "IsSelected" property live?

  1. In the Model? (I dont like this idea, as the model now is exposing a property that is only required by a view and has nothing to do with the actual domain object, also, this property would be useless, if I implemented the view differently and didnt allow deletion of multiple employees at once).
  2. In the "EmployeeListViewModel" as a separate Dictionary collection, that would track whether an employee is selected or not? (Or even just a HashSet containing all selected employees). I dont like this much as the binding in the view is no longer straight forward.
  3. Implement a separate EmployeeViewModel, that wraps the Employee object and exposes the IsSelected property. The EmployeeListViewModel then will expose its collection as a ObservableCollection. I like this solution the best, but I always thought that there is one ViewModel per View and in this case, I have 2 view-models for my view. Is that a deviation from the MVVM pattern or is this the typical way to implement MVVM? (references?)

解决方案

Create a reusable Generic SelectableItem that wraps each item in the EmployeeList:

Simple example:

public class SelectableItem<T>: INotifyPropertyChanged
{
    public bool IsSelected {get;set;} //PropertyChanged(), etc

    public T Value {get;set;}
}

then in the ViewModel:

public ObservableCollection<SelectableItem<Employee>> Employees {get;set;}

and in the View:

<DataTemplate>
   <CheckBox IsChecked="{Binding IsSelected}" Content="{Value.FullName}"/>
</DataTemplate>

Then you can retrieve all selected employees just by:

var selectedemployees = Employees.Where(x => x.IsSelected).Select(x => x.Value);

这篇关于仅查看属性(例如:IsSelected)和MVVM中的Model的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆