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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:38:40+00:00 2026-06-17T00:38:40+00:00

MVVM methodology of WPF for problem specified. I have a combo box say in

  • 0

MVVM methodology of WPF for problem specified. I have a combo box say in a ‘MainView’ of XAML. Its code partner is ‘MainViewModel’ and exposes a property of a ‘Person’ which is basically just a seperate class(POCO class) for exposing a string and an int to represent a name and a seed in the database. It sets up a property of ReadOnlyCollection that binds to a combobox like so: (referencing the viewmodel at the top of the xaml like: xmlns:vm=”clr-namespace:(mylocationforviewmodelnamespace))

ItemsSource="{Binding Path=People}"
                    DisplayMemberPath="FirstName"
                    SelectedValuePath="PersonId"

This works great but I am then setting up a User Control View and it’s respective View Model code. What I am not getting about the ViewModel method of binding is how you bind the passed in values for the constructor? Or can you even do that? Or should I be setting up an intermediary class not just for my ‘model’ but for the ‘DataAccess’ ?

My end goal is to select a value in a combobox, which is already bound properly and works great, and pass it to the viewmodel code that then associates with the view when built and docks in the parent form. I can make a constructor just fine and set a static value to make a name appear when it builds. I don’t know how you pass the value from the parent view object of a combobox which is bound to the resulting creation of usercontrol. I am up for doing lots of things but I really want to stick to the MVVM method and not do this in code behind of which I already know how to do this.

MVVM method I am following loosely is here: http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

  • 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-06-17T00:38:42+00:00Added an answer on June 17, 2026 at 12:38 am

    Okay I got it. The MainViewModel in the example contains private methods which are the constructors of the subsequent user controls. The main window binds to the combobox fine but needs more detail on the member of the combobox it needs to relate to a new constructor. This needs to be set and bound to a property bound to the element in XAML ‘selected value’ of the combobox. Once this property knows it is bound it can later on be used in an internal method in the ViewModelCode that is databound to inform the constructor of what the passed in Person object needs to be.

    I see a lot of people with situations similar to mine but different so I thought I would post this if anyone may find it useful later. The only word I would add is that I believe you need to inherit “INotifyPropertyChanged” class but in my example it is in an abstract class two classes inheritance levels down so I felt it was not necessary to redo everything to show a simpler example as I got what I needed.

    XAML:

    <ComboBox Height="30" Width="170" Margin="10" x:Name="combopersons" 
                        FontSize="20"
                        ItemsSource="{Binding Path=People}"
                        DisplayMemberPath="FirstName"
                        SelectedValuePath="PersonId"
                        SelectedValue="{Binding Path=CurrentUser}"  />
    

    Fields:

    Person _currentPerson;
            ReadOnlyCollection<Person> _people;
            ObservableCollection<WorkspaceViewModel> _workspaces;
    
            string _curuser;
            string u = WindowsIdentity.GetCurrent().Name.Split('\\')[1];
            public string CurrentUser { get; set; }
    
            ExpensesEntities ee = new ExpensesEntities();
    
    public ReadOnlyCollection<Person> People
            {
                get
                {
                    if (_people == null)
                    {
                        List<Person> persns = this.GetPeople();
                        _people = new ReadOnlyCollection<Person>(persns);
                    }
                    return _people;
                }
            }
    

    Constructor:

    public MainWindowViewModel()
            {
                _curuser = ee.tePersons.Where(n => n.FirstName == u)
                            .Select(x => x.PersonID).FirstOrDefault().ToString();
    
                CurrentUser = _curuser;
            }
    

    Helper Methods:

            List<Person> GetPeople()
            {
                //ExpensesEntities ee = new ExpensesEntities();
                return ee.tePersons.Select(x => new Person
                                             {
                                                 PersonId = x.PersonID,
                                                 FirstName = x.FirstName
                                             }).ToList();
            }
    
    
    int ConvertToNumber(string s)
            {
                try
                {
                    return Convert.ToInt32(s);
                }
                catch (FormatException e)
                {
                    return 0;
                }
            }
    
            void SetCurrentUser()
            {
                int currentID = ConvertToNumber(CurrentUser);
    
                _currentPerson = ee.tePersons
                                   .Where(i => i.PersonID == currentID)
                                   .Select(p => new Person
                                                    {
                                                        PersonId = p.PersonID,
                                                        FirstName = p.FirstName
                                                    }).FirstOrDefault();
            }
    

    Constructor in MainViewModel of CHILD View Model:

        void MoneyEntry()
        {
            SetCurrentUser();
            MoneyEntryViewModel money = new MoneyEntryViewModel(_currentPerson);
            this.Workspaces.Add(money);
            this.SetActiveWorkspace(money);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using MVVM Light, I have two WPF applications that reference a common Views library.
I'm familiar with WPF technology, specifically MVVM methodology. Recently I began to program in
I'm experimenting with MVVM in the context of a simple WPF app. I have
I'm using MVVM in my WPF application. I have a Window that has a
I have large WPF-MVVM application in which I have 1 subscribe and 2 publish
I have been learning MVVM / WPF and have gone through the tutorial here
Has anyone got a TDD-ish methodology for designing complex WPF xaml components (i.e., ControlTemplates,
I have a wpf-mvvm application. I have one textbox and one button (similar to
Considering you have an MVVM Architecture in WPF like Josh Smith's examples How would
In following the MVVM architecture for WPF, learning WPF DataBindings. I have a single

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.