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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:26:48+00:00 2026-06-01T12:26:48+00:00

I am trying my hands on WPF MVVM. I have written following code in

  • 0

I am trying my hands on WPF MVVM. I have written following code in XAML

<UserControl x:Class="Accounting.Menu"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
             xmlns:local="clr-namespace:Accounting"
             mc:Ignorable="d" 
             d:DesignHeight="105" d:DesignWidth="300">
    <UserControl.DataContext>
        <local:MenuViewModel/>
    </UserControl.DataContext>
    <StackPanel>
       <StackPanel>
                <TextBlock Text="{Binding Path=MenuHeader}"/>
       </StackPanel>
       <ListBox ItemsSource="{Binding Path=MenuItems}" Height="70"/>             
    </StackPanel>     
</UserControl>

I have got a MenuViewModel with properties MenuHeader and MenuItems. I get values in both the properties during runtime. Former is bound to text of TextBlock and latter to ItemSource of ListBox. But when I run the solution, TextBlock and ListBox are empty.

Edit: Code of ViewModel

  public class MenuViewModel: ViewModelBase
    {
        AccountingDataClassesDataContext db;

        private string _menuType;
        public string MenuHeader { get; set; }
        public ObservableCollection<string> MenuItems { get; set; }

        public MenuViewModel()
        { 

        }

        public MenuViewModel(string menuType)
        {
            this._menuType = menuType;
            db = new AccountingDataClassesDataContext();
            if (menuType == "Vouchers")
            {
                var items = db.Vouchers.OrderBy(t => t.VoucherName).Select(v => v.VoucherName).ToList<string>();

                if (items.Any())
                {
                    MenuItems = new ObservableCollection<string>(items);
                    MenuHeader = "Vouchers";
                }
            }
            else
            {
                System.Windows.MessageBox.Show("Menu not found");
            }

        }
    }

Thanks in advance.

  • 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-01T12:26:50+00:00Added an answer on June 1, 2026 at 12:26 pm

    You are creating your ViewModel in the XAML using your ViewModel’s default contructor which does nothing. All your population code is in the non-default contructor which is never called.

    The more usual way is to create the ViewModel in code, and inject it into the view either explicitly using View.DataContext = ViewModel, or impllcitly using a DataTemplate.

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

Sidebar

Related Questions

So I'm trying my hands at a css menu and I have a simple
I have been trying my hands on opensource tool JUnit for testing Java Code
Lately I have been trying my hands on Eclipse IDE for java development. I
I have a mystery on my hands. I am trying to learn managed C++
I am trying hands on one code in C where in I need to
I am trying my hands on Socket programming in C# (.NET). I have referred
I'm a novice WPF programmer. I'm trying to add some structure to my code:
I am trying my hands on MVC 2, ADO.NET EF and POCO. I have
I am trying my hands on lettuce with splinter. I have a django app
I am trying to get my hands on learning Visual force. I have an

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.