Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6187705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:06:17+00:00 2026-05-24T02:06:17+00:00

I am working on a WPF MVVM framework I have a ItemType Model public

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T02:06:18+00:00Added an answer on May 24, 2026 at 2:06 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working in wpf - mvvm model. I have a textbox which holds-
I'm building a WPF application and working with the MVVM pattern. I have 4
I have the following DTO definition:- [DataContract] public class AddProductDTO { [DataMember] public string
I'm working on an WPF application using the mvvm-light framework. I'm new to both
I am working on a project where I have implemented MVVM for WPF desktop
I have been working through some of the examples of MVVM & WPF and
I am working on my first WPF/MVVM application, and I have come across a
I'm working on my first true WPF MVVM pattern application. Currently I have a
I'm working on a WPF MVVM application and I've got a TextBox on my
I am new to WPF and MVVM, and I am working on an application

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.