I am working on a WPF MVVM framework
I have a ItemType Model
public class ItemType
{
public long ItemTypeID { get; set; }
public long ItemCategoryID { get; set; }
public string Name { get; set; }
}
and other ItemCategory Model
public class ItemCategory
{
public long ItemCategoryID { get; set; }
public string Name { get; set; }
}
Now I want to Bind ItemType to a data grid, But I don’t want to show ItemCategoryID. I want to show ItemCategory.Name
How can this be done without changing my original class?
This is what MVVM uses the ViewModel for.
Do not modify the Model but instead create ViewModel classes that are structured according to the needs of the View. that is where you would leave out the properties you don’t want.
EDIT
Here is a way of doing that:
Use the static method to create instances of the
ItemTypeViewModelfor each ItemType in the model. Put them all in the ‘Collection’ property of theItemTypeSViewModel.Bind the DataGrid to the Collection.
I ‘removed’ the relation that exists in your model between ItemType and ItemCategory from my ViewModel. This is just one way of handling such a construction. Instead you could create a ViewModel class for the ItemCategory too and have a reference in the ItemTypeViewModel class to an instance of the ItemCategoryViewModel class.
Note that this is just one way of handling this. You could solve this problem in some other ways too. Also: you will need to provide a transformation from the ViewModel classes back to the Model as well.
A final bit of advise: if this is new to you start reading/watching tutorials on MVVM: https://stackoverflow.com/questions/2267903/learning-mvvm-for-wpf