I have a Customer class generated from LINQ. This class is being used in many places. I can bind a collection of Customers to a DataGrid (开发者_运维百科WPF). I would like to add an extra column to the DataGrid. The column data would depend on how the Customer class is used. Here are the options I have tried:
- Add the extra property in the Customer class. This works but the property does not belong here since it may depend on how the class is used.
- Create another class that derives from Customer. This does not work as we cannot cast Customer to the derived class and a list of Customers is what I get back from database.
- Create another class that contains a Customer and reimplements all the relevant properties. This is ugly.
- Create an extension method for the desired property. Unfortunately, we cannot bind a DataGrid column to an extension method.
At this point, these options either do not work or are not ideal. I would appreciate any suggestion.
This sounds like the job of a view model, so I would favour option 3, and create a CustomerViewModel
which wraps your Customer
model and adds the additional property you require. I'm assuming that this additional property is just for display purposes only which is why you are reluctant to add it to the Customer
model.
精彩评论