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 908547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:44:03+00:00 2026-05-15T16:44:03+00:00

I have a class : public class Car { public string Name { get;

  • 0

I have a class :

public class Car
{        
    public string Name { get; set; }
    public int Age { get; set; }
    public Wheel[] Wheels {get;set;}       
}

Collections of wheels can be changed

Every properties of Car will be showing at the same control

I want to see how make a ViewModel for this class.

Had I good understood conception of MVVM ?

public class CarViewModel()
{
ObservableCollection<Wheel> _wheels{ get; set; }
Car _car {get;set;}

public ObservableCollection<Wheel> Wheels
{
get{ return this._car.Wheels;}
set{ this._car.Wheels=value}
}

public string Name
{
get{ return this._car.Name;}
set{ this._car.Name=value}
}


public int Age
{
get{ return this._car.Age;}
set{ this._car.Age=value;}
}
public CarViewModel()
{
this._car=GetCar();
}
}
}

I paste code of my application where I have problem with this MVVM(I’m not sure of good implementation, And removing Facility doesn’t work. Could you help me rebuild this code?

Part of UserControlHotelDescription.xaml

<ListBox Grid.Column="1" ItemsSource="{Binding Path=CheckedRoomFacilities}">
                            <ListBox.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <CheckBox IsChecked="{Binding Path=IsChecked}" Click="CheckBox_Click"/>
                                    <TextBlock Text="{Binding Path=Name}" />
            </StackPanel>
                            </DataTemplate>
                        </ListBox.ItemTemplate>

                    </ListBox>

public partial class UserControlHotelDescription : UserControl
    {
        public UserControlHotelDescription()
        {
            InitializeComponent();
        }


        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {

            CheckBox checkBox = sender as CheckBox;
            CheckableFacility checkableFacility = checkBox.DataContext as CheckableFacility;

            if (checkBox.IsChecked.GetValueOrDefault())
            {
                ((HotelDescriptionModelView)this.DataContext).RoomFacilities.Add(new Facility() { Name = checkableFacility.Name,Id=checkableFacility.Id });
            }
            else
            {

            ((HotelDescriptionModelView)this.DataContext).RoomFacilities.Remove(((HotelDescriptionModelView)this.DataContext).RoomFacilities.Where(ce => ce.Id == checkableFacility.Id).First());
            }
        }
    }

public class HotelDescriptionModelView
    {
        public HotelDescription HotelDescription { get; set; }

        List<Facility> AvailableFacilities { get; set; }

        public HotelDescriptionModelView()
        {

//there is Equal to: HotelDescription =GetHotelDescription() 
HotelDescription = new HotelDescription();
            HotelDescription.Name = "Hilton";

            List<Facility> gotFacilities = new List<Facility>();
            gotFacilities.Add(new Facility() { FacilityCategory = FacilityCategory.Relax, Id = 1, Name = "Jacuzzi", FacilityType = FacilityType.Room });
            gotFacilities.Add(new Facility() { FacilityCategory = FacilityCategory.Sport, Id = 2, Name = "Trash", FacilityType = FacilityType.Room });
            gotFacilities.Add(new Facility() { FacilityCategory = FacilityCategory.Sport, Id = 4, Name = "Chairlift", FacilityType = FacilityType.SkiSlope });

            HotelDescription.Facilities = gotFacilities.ToArray();

            AvailableFacilities = new List<Facility>();
            AvailableFacilities.Add(new Facility() { FacilityCategory = FacilityCategory.Relax, Id = 1, Name = "Jacuzzi", FacilityType = FacilityType.Room });
            AvailableFacilities.Add(new Facility() { FacilityCategory = FacilityCategory.Relax, Id = 2, Name = "Trash", FacilityType = FacilityType.Room });
            AvailableFacilities.Add(new Facility() { FacilityCategory = FacilityCategory.Relax, Id = 3, Name = "AirCondition", FacilityType = FacilityType.Room });
            AvailableFacilities.Add(new Facility() { FacilityCategory = FacilityCategory.Relax, Id = 4, Name = "Chairlift", FacilityType = FacilityType.SkiSlope });
            AvailableFacilities.Add(new Facility() { FacilityCategory = FacilityCategory.Relax, Id = 5, Name = "T-bar lift", FacilityType = FacilityType.SkiSlope });

        }
        public string Name
        {
            get
            {
                return this.HotelDescription.Name;
            }
            set
            {
                this.HotelDescription.Name = value;
            }
        }

        public List<Facility> RoomFacilities
        {
            get
            {

                return
                    this.HotelDescription.Facilities.Where(
                        f => f.FacilityType == FacilityType.Room).ToList();

            }
            set
            {
                this.RoomFacilities = value;

            }
        }
        public CheckableFacility[] CheckedRoomFacilities
        {
            get
            {

                return (from fl in AvailableFacilities.Where(af => af.FacilityType == FacilityType.Room).Union(RoomFacilities)
                        group fl by fl.Name into d
                        select new CheckableFacility()
                                   {
                                       Name = d.Key,
                                       IsChecked = (d.Count() > 1)
                                   }).ToArray();

            }
            set
            {

                this.CheckedRoomFacilities = value;

            }
        }


    }

public class CheckableFacility
    {
        public int Id { get; set; }

        public FacilityCategory ReporterFacilityCategory { get; set; }

        public bool IsChecked { get; set; }

        public string Name { get; set; }


    }
  • 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-15T16:44:03+00:00Added an answer on May 15, 2026 at 4:44 pm

    Are you going to create a ViewModel for each of your model? I’m afraid.

    ViewModel should be created for each view which would have all the properties to which the View’s elements could be bound to.

    If the property is a data from your db, it would be in the model as a property and could be used by the viewmodel without creating any member. Else your viewmodel has nothing to do with the model.

    Once you’ve created the Model, keep it aside. Then create the view. Check what are the data required for the binding. Create as many properties in the ViewModel as required by the view. Most of the data would be dependent on the Model (may be different models) or the derivation of the data in the model. Hence create the required model instances so that their properties could be get and set through ViewModel.

    If you still couldn’t get a clear picture, send me your view. I will create a sample viewmodel out of it.

    Update

    HotelDescriptionModelView:

    /* This ViewModel is to serve UserControlHotelDescription */
    public class HotelDescriptionModelView 
    {
        HotelDescription _hotelDescriptionModel;
        public ObservableCollection<CheckableFacilityViewModel> CheckableRoomFacilities { get; set; }
    
        public HotelDescriptionModelView()
        {
            _hotelDescriptionModel = new HotelDescription();
            CheckableRoomFacilities = new ObservableCollection(GetCheckableFacilities(FacilityType.Room)); 
        }
    
        IEnumerable<CheckableFacilityViewModel> GetCheckableFacilities(FacilityType type)
        {
            return (
                        from fl in AvailableFacilities.Where(af => af.FacilityType == type)
                                                      .Union(RoomFacilities) 
                        group fl by fl.Name into d 
                        select new CheckableFacilityViewModel() 
                        { 
                            Name = d.Key, 
                            IsChecked = (d.Count() > 1) 
                        }
                    );
        }
    }
    

    CheckableFacilityViewModel:

    /* CheckableFacility has to be a viewmodel to serve the 
       ListViewItem in the UserControlHotelDescription (and other views) */
    public class CheckableFacilityViewModel
    { 
        public int Id { get; set; }  /* I'm not sure if you use Id in your View */
        public FacilityCategory ReporterFacilityCategory { get; set; }  
        public bool IsChecked { get; set; } 
        public string Name { get; set; }  
    } 
    

    CheckableFacilityView:

    /* The ListViewItem has to be another View(usercontrol) 'CheckableFacilityView' */
    <UserControl>
        <StackPanel Orientation="Horizontal"> 
            <CheckBox IsChecked="{Binding Path=IsChecked}"/> 
            <TextBlock Text="{Binding Path=Name}" />
        </StackPanel>
    </UserControl>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 483k
  • Answers 483k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Maybe you should transform your user object to a JSON.… May 16, 2026 at 7:05 am
  • Editorial Team
    Editorial Team added an answer You can use "LoaderInfo" object, with "Event.COMPLETE". Ex: stage.loaderInfo.addEventListener(Event.COMPLETE,onComplete); function… May 16, 2026 at 7:05 am
  • Editorial Team
    Editorial Team added an answer I made a jsfiddle that uses jQuery and jQuery UI… May 16, 2026 at 7:05 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.