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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:28:51+00:00 2026-05-26T20:28:51+00:00

I am following MVVM Pattern and I want to bind ListView ItemsSource using XAML,

  • 0

I am following MVVM Pattern and I want to bind ListView ItemsSource using XAML, not in even
this.Datacontext = ObservableCollection property.

My code is like this:

            <ListView x:Name="MenuBarList" 
                  Grid.Row="2"
                  ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
                  ItemsSource="{Binding Path=Menu.Option}"
                  Width="{Binding MainMenuWidth}"
                  SelectedItem="{Binding Path=SelectedMainMenuOption, Mode=TwoWay}" >
            <ListView.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel Orientation="Vertical" />
                </ItemsPanelTemplate>
            </ListView.ItemsPanel>

            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Title}" TextWrapping="Wrap" IsHitTestVisible="False" />
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

and the Menu is the property and it will seat on the ViewModel. Option is the Class of the Property, so I am using Menu.Option

My Menu is the property of ContentMenuModel type and ContentMenuModel is the class which contain the property of Option and Title and Image.

See the property of the Menu which is inside of the ViewModel

        public const string MenuPropertyName = "Menu";

    private ContentMenuModel _Menu = null;

    /// <summary>
    /// Gets the Menu collection.
    /// Changes to that property's value raise the PropertyChanged event. 
    /// </summary>
    public ContentMenuModel Menu
    {
        get
        {
            return _Menu;
        }

        set
        {
            if (_Menu == value)
                return;

            _Menu = value;

            // Update bindings, no broadcast
            RaisePropertyChanged(MenuPropertyName);
        }
    }

And the ContentMenuModel class looks like this:

    public class ContentMenuModel 
{

    #region Title

    /// <summary>
    /// The <see cref="Title" /> property's name.
    /// </summary>
    public const string TitlePropertyName = "Title";

    private string _Title = String.Empty;

    /// <summary>
    /// Gets the Title property.
    /// Changes to that property's value raise the PropertyChanged event. 
    /// </summary>

    [Required]
    [StringLength(128, ErrorMessage = "The Title value cannot exceed 128 characters. ")]
    public string Title
    {
        get
        {
            return _Title;
        }

        set
        {
            if (_Title == value)
            {
                return;
            }

            var oldValue = _Title;
            _Title = value;

            // Update bindings, no broadcast
            RaisePropertyChanged(TitlePropertyName);
        }
    }

    #endregion

    #region Options

    /// <summary>
    /// The <see cref="Options" /> property's name.
    /// </summary>
    public const string OptionsPropertyName = "Options";

    private ObservableCollection<ContentMenuOptionModel> _Options = null;

    /// <summary>
    /// Gets the Options property.
    /// Changes to that property's value raise the PropertyChanged event. 
    /// </summary>

    public ObservableCollection<ContentMenuOptionModel> Options
    {
        get
        {
            return _Options;
        }

        set
        {
            if (_Options == value)
            {
                return;
            }

            var oldValue = _Options;
            _Options = value;

            RaisePropertyChanged(OptionsPropertyName);
        }
    }

    #endregion

    #region ContextText

    /// <summary>
    /// The <see cref="Options" /> property's name.
    /// </summary>
    public const string ContextTextPropertyName = "ContextText";

    private ContentPageItem _ContextText = null;

    /// <summary>
    /// Gets the ContextText property.
    /// Changes to that property's value raise the PropertyChanged event. 
    /// </summary>

    public ContentPageItem ContextText
    {
        get
        {
            return _ContextText;
        }

        set
        {
            if (_ContextText == value)
            {
                return;
            }

            _ContextText = value;

            RaisePropertyChanged(OptionsPropertyName);
        }
    }

    #endregion
}

I had binded the ViewModelLocator to my mainwindow’s DataContext and Path=ViewModel’s MainMenu, so the MainMain is the object of the ViewModel where I can bind this Property to the ListView’s ItemsSource, but it is not working.

Please correct me where I am wrong.

  • 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-26T20:28:52+00:00Added an answer on May 26, 2026 at 8:28 pm

    In order to get full example quickly, you can install NUGet for Visual Studio and install MVVMLight package through it on a clean WPF Application project. Then it will set everything up, and you will see how it works.
    Though I’ll describe the basics here.
    MVVMLight supports this out of the box. Standard template for MVVMLigth includes ViewModelLocator and a MainViewModel. ViewModelLocator – is the class that holds all other view models. From the start it has only one property public MainViewModel Main {get;}. ViewModelLocator is registered as a resource in App.xaml

    <Application>
      <Application.Resources>
        <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
      </Application.Resources>
    </Application>
    

    Then, on any page, if you want to get to the view model, you should simply reference Locator resource and get its property that is appropriate for your page. Here’s example for MainWindow and its MainViewModel:

    <Window DataContext="{Binding Source={StaticResource Locator}, Path=Main}">
        <Grid>
            <TextBlock Text="{Binding Text}"/>
        </Grid>
    </Window>
    

    In the example above, I’ve added public string Text {get;} property to MainViewModel and referenced. In example there is no need for any code-behind, everyting is setup declaratively via xaml.

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

Sidebar

Related Questions

I'm writing a WPF application using the MVVM pattern, based on the following article:
I'm using C# WPF following the MVVM design pattern. Currently my View has a
I'm currently using Silverlight 4 and following the MVVM pattern. I have login boxes
I'm using the MVVM pattern and am receiving the following when i run my
I am using the MVVM pattern for a WPF application. In several places I
I'm developing a WPF application using the MVVM pattern and I need to display
In a WPF app that I'm writing using the MVVM pattern, I have a
I am currently learning the MVVM pattern, and the tutorial I am following uses
Following the MVVM pattern I'm trying to wire up the display of a child
I am designing a very simple C# WPF application, following the MVVM pattern, with

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.