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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:51:43+00:00 2026-06-17T12:51:43+00:00

I currently have a simple applications consisting of two synched ListBoxes bound to ObservableCollections

  • 0

I currently have a simple applications consisting of two synched ListBoxes bound to ObservableCollections which wrap two different Entity Framework classes.

When a Part item in listBox1 is selected, it passes the SelectedItem ‘s navigation key information to listBox2, which displays the respective subset of Vendors entities.

Currently, my ViewModel (MainViewModel.cs) looks like:

public MainViewModel()
{
    _context = new DBEntities();
    _partsCollection = new ObservableCollection<Part>(_context.Parts);
    _vendorsCollection = new ObservableCollection<Vendor>(_context.Vendors);
}

public ObservableCollection<Part> PartsCollection
{
     get { return _partsCollection; }
     set
     {
          OnPropertyChanged("PartsCollection");
          _partsCollection = value;
     }
}


public Observable<Part> SelectedPart
{
     get { return _selectedPart; }
     set
     {
          OnPropertyChanged("SelectedPart");
          _selectedPart = value;
     }
}

public ObservableCollection<Vendor> VendorsCollection
{
     get { return _vendorsCollection; }
     set
     {
          OnPropertyChanged("VendorsCollection");
          _vendorsCollection = value;
     }
}

and my view (MainView) looks like:

    <UserControl.Resources>
    <local:MainViewModel x:Key="MainViewModelDataSource" />
    </UserControl.Resources>

    <Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource MainViewModelDataSource}}">
           <ListBox ItemsSource="{Binding PartsCollection, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"  Margin="43,87,377,57" Name="listBox1"
             SelectedItem="{Binding SelectedPart, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
             IsSynchronizedWithCurrentItem="True" >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock FontSize="13" Foreground="Black" Padding="3" Text="{Binding shapeName}" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

    <ListBox IsSynchronizedWithCurrentItem="True" 
             ItemsSource="{Binding ElementName=listbox2,  Path=SelectedItem.Vendors, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True}" Margin="345,87,75,57" 
             >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock FontSize="13" Foreground="Black" Padding="3" Text="{Binding mateStyle}" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox> 

This works great. However, I would like to bind listBox2 to properties of my viewmodel to update the VendorsCollection. I would like to use the “SelectedPart” property on my viewmodel, which isn’t even currently being used right now.

The whole point of EF is to utilize all of the capabilities of it as an ORM, not build an additional ORM again in my viewmodel just to send change notifications. From what I’ve seen, setting up an ICollectionView is a rather round about method, but I was unable to figure out the bindings.

I’m unsure what the next step is so that I can raise PropertyChanged notifications and update my Vendors ObservableCollection, binding the listBox2 xaml to collection properties of my viewmodel.

  • 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-17T12:51:44+00:00Added an answer on June 17, 2026 at 12:51 pm

    I’m using MVVM and Entity Framework too, but with code first approach. I was tired of all the MVVM “purity”, in which the ViewModel should raise the change notification events, so I implemented INPC on all my model classes and I’ve never been happier before! It’s just crazy to duplicate all your model properties in your ViewModels just to notify changes.

    I see you are using DBEntities, so I’m not sure how you would implement change notification in your model objects, but I strongly recommend that you do so. I will make your life much easier.

    By the way, be sure to raise the OnPropertyChanged event AFTER you set the backing field to the new value, like this:

     set
     {
          _selectedPart = value; //first you change the private backing field
          OnPropertyChanged("SelectedPart"); //then you notify that it has changed, so everything gets the new value
     }
    

    Regarding the binding, you ARE using the SelectedPart property of your ViewModel, here:

    SelectedItem="{Binding SelectedPart, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
    IsSynchronizedWithCurrentItem="True"
    

    You are binding the selected item in the listbox1 to the SelectedPart property, and by setting the IsSynchronizedWithCurrentItem to true, every time you select something in the UI the property in the ViewModel gets updated (and the other way too, if you set the SelectedPart in code the UI will receive the update).

    So, to bind to the SelectedPart’s vendors in the second listbox just do this:

    ItemsSource=”{Binding SelectedPart.Vendors, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True}”

    In this case you don’t need to create the VendorsCollection, because you can access the same elements via SelectedPart.Vendors (or you can set the VendorsCollection just to be a wrapper with a nice name to return SelectedPart.Vendor, just make sure to check SelectedPart for null).

    Regarding the ICollectionView, know that it is only a wrapper around the ObservableCollection (or similar) in order to bind to FrameworkElements (like the ListBox) that inherit from Selector (that is, that they have the SelectedItem property).

    WPF creates an ICollectionView in the background when you bind to the ObservableCollection, so you get the SelectedItem. You can create your own ICollectionView, feed it with your ObservableCollection, and bind to the ICollectionView in XAML just the same way you do with the ObservableCollection. If you decide to do so, I recommend that you use a ListCollectionView, with which you can filter and create groups (for use with treeviews).

    One final word: remember that the ObservableCollection (and all the ICollectionViews) only raise CollectionChanged events (that is, when you add/remove items to/from the collection). They will not raise anything if you change a property of an element that belongs to the collection, even if it implements INotifyPropertyChanged. If you want to catch that you’ll have to create a custom collection, one (of many) example here.

    Hope this is useful, regards!

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

Sidebar

Related Questions

I am currently in the process of developing two iOS applications which heavily rely
I currently have a number of web applications which access a common service running
I have a simple ticket logging application build on LAMP. I am currently playing
I'm currently working on a simple web application where users each have their own
I'm writing a simple shopping cart application and have hit a road block. Currently
I currently have a simple form that when you click the save button will
Currently I have a simple knockoutJS object, with a few observables. But this object
Currently I have a simple maven project that is building a jar file and
I currently have a very simple MySQL database (articlesDB) with 1 table (articles) and
I currently have a Perl CGI script that parses incoming XML requests using XML::Simple

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.