开发者

Extend My Models

开发者 https://www.devze.com 2023-03-24 13:29 出处:网络
I am working on a WPF MVVM framework I have a ItemType Model 开发者_如何转开发public class ItemType

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:

public class ItemTypeViewModel : INotifyPropertyChanged
{
    private string _name;
    public string Name
    {
        get { return _name; }
        set
        {
            if (_name != value)
            {
                _name = value;
                OnPropertyChanged("Name");
            }
        }
    }

    private string category;
    public string Category
    {
        get { return category; }
        set
        {
            if (category != value)
            {
                category = value;
                OnPropertyChanged("Category");
            }
        }
    }

    public static ItemTypeViewModel FromModel(ItemType model)
    {
        var itemTypeViewModel = 
            new ItemTypeViewModel 
            {
                Name = model.Name,
                Category = categories[model.CategoryID].Name;
            };
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        var p = PropertyChanged;
        if (p != null)
        {
            p(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

public class ItemTypesViewModel : ObservableCollection<ItemTypesViewModel>
{
    private ObservableCollection<ItemTypeViewModel> _collection;

    public ObservableCollection<ItemTypeViewModel> Collection
    {
        get { return _collection; }
        set
        {
            if (_collection != value)
            {
                _collection = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Collection"));
            }
        }
    }
}

Use the static method to create instances of the ItemTypeViewModel for each ItemType in the model. Put them all in the 'Collection' property of the ItemTypeSViewModel.

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


The answer provided by both guys are perfectly valid and fit your needs, as much as I understood your question. The basic idea is:

@Amani:

//check if the query fits your needs
var temp = from i in categoryList
                   from it in itemList
                   where i.ItemCategoryID == it.ItemCategoryID 
                   select new { i, it };

Make a cross query to generate a new composed type, which can be

public class ItemTypeViewModel
{
    public long ItemTypeID { get; set; }
    public string ItemCategoryName { get; set; }
    public string ItemTypeName { get; set; }
}

this is according to MVVM patter guideline responded by @Erno. Use a collection of ItemTypeViewModel object to bind them to your view.

Hope this helps.

Regards.


If this is your database model you can easily make a view for join those tables. or you can use this following code as pattern :

        var temp = from i in categoryList
                   from it in itemList
                   where i.ItemCategoryID == it.ItemCategoryID 
                   select new { i, it };

Hope this help.

0

精彩评论

暂无评论...
验证码 换一张
取 消