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

The Archive Base Latest Questions

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

I am attempting to write my first implementation of databinding in a WPF form.

  • 0

I am attempting to write my first implementation of databinding in a WPF form.

I have got to the point where I can successfully retrieve data to populate my combobox with the required entries but am stuck on how I would go about populating a dependent ListView given the current selection of the Combobox.

To explain my problem better this is the current view backing my application.

class SchoolBookViewModel
{
    public SchoolBookViewModel() { }

    //Problem is here... How do I pass in the SelectedSchoolClassID?
    public CollectionView ClassBookListEntries(Guid SelectedSchoolClassID)
    {
        return new CollectionView(SchoolQuoteList.GetSchoolClassQuoteListBySchoolClassID(SelectedSchoolClassID, 2011, "MyConnectionString"));
    }

    public CollectionView SchoolEntries
    {
        get
        {
            return new CollectionView(SchoolList.GetSchoolList("MyConnectionString"));
        }
    }

}

And this is the XAML

<StackPanel Height="449" HorizontalAlignment="Left" Margin="12,12,0,0" Name="stackPanel1" VerticalAlignment="Top" Width="650" DataContext="{Binding}">
      <Label Content="School:" Height="28" Name="lblSchoolName" Width="651" />
      <ComboBox Height="23" Name="cmbSchoolNames" Width="644" DisplayMemberPath="SchoolName" ItemsSource="{Binding Path=SchoolEntries}" SelectedValuePath="SelectedSchoolClassID" SelectionChanged="cmbSchoolNames_SelectionChanged" />
      <Label Content="Class booklist:" Height="29" Name="label1" Width="651" />
      <ListView Height="163" Name="lblClassBookList" Width="645" ItemsSource="{Binding Path=ClassBookListEntries}" DisplayMemberPath="QuoteReference" />
</StackPanel>

And in the Window_Loaded method I call

stackPanel1.DataContext = new Views.SchoolBookViewModel(); 

Am I even on the right track? Any guidance would be appreciated.

  • 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-20T16:53:45+00:00Added an answer on May 20, 2026 at 4:53 pm

    To get the ComboBox’s selection back into the ViewModel you need a property to bind to one of its selection properties. You can also get rid of the explicit CollectionViews if you’re not doing anything with them. By just binding to the collections directly, ICollectionView instances will be created and managed for you automatically. Try structuring your VM like this:

    class SchoolBookViewModel : INotifyPropertyChanged
    {
        private SchoolList _selectedSchoolClass;
        public SchoolList SelectedSchoolClass
        {
            get { return _selectedSchoolClass; }
            set
            {
                _selectedSchoolClass = value;
                ClassBookListEntries = SchoolQuoteList.GetSchoolClassQuoteListBySchoolClassID(_selectedSchoolClass.Id, 2011, "MyConnectionString");
                NotifyPropertyChanged("SelectedSchoolClass");
            }
        }
    
        private SchoolQuoteList _classBookListEntries;
        public SchoolQuoteList ClassBookListEntries
        {
            get { return _classBookListEntries; }
            set
            {
                _classBookListEntries = value;
                NotifyPropertyChanged("ClassBookListEntries");
            }
        }
    
        private SchoolList _schoolEntries;
        public SchoolList SchoolEntries
        {
            get
            {
                if (_schoolEntries == null)
                    _schoolEntries = SchoolList.GetSchoolList("MyConnectionString");
                return _schoolEntries;
            }
        }
        ...
    }
    

    In general it’s better to not set explicit Width and Height values and instead let the layout system size elements for you. You can also get rid of the DataContext=”{Binding}” – this is redundant as DataContext is inherited and {Binding} means the value of the DataContext itself. Here’s the XAML cleaned up and bound to the new properties from above:

    <StackPanel HorizontalAlignment="Left" Margin="12,12,0,0" x:Name="stackPanel1" VerticalAlignment="Top">
        <Label Content="School:" x:Name="lblSchoolName" />
        <ComboBox x:Name="cmbSchoolNames" DisplayMemberPath="SchoolName" ItemsSource="{Binding Path=SchoolEntries}"
                  SelectedItem="{Binding SelectedSchoolClass}" />
        <Label Content="Class booklist:" x:Name="label1" />
        <ListView x:Name="lblClassBookList" ItemsSource="{Binding Path=ClassBookListEntries}" DisplayMemberPath="QuoteReference" />
    </StackPanel>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to write my first Dynamics Crm 2011 plugin. I have therefore
I have been attempting to write a VBA Script that can parse out other
I'm attempting to write my first custom Cocoa Framework and I want to expose
I'm attempting to write my first jQuery plugin to query multi-dimensional arrays of complex
I'm attempting to write a TinyURL like clone in ASP.NET MVC as a first
I'm attempting to write my first unity script. This is the code for a
I am attempting to write a JQuery to focus on the first visible, enabled
I'm attempting to write my first php/mysql page and am running into a SQL
I'm attempting to write a widget for DotNetNuke in C#. This is my first
I attempting to write a simple JQuery search. This is what I have so

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.