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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:51:01+00:00 2026-05-25T19:51:01+00:00

I would appreciate it if somebody could tell me how to bind a listbox

  • 0

I would appreciate it if somebody could tell me how to bind a listbox to the entity framework (EF code first).

I’m using prism and mef. There are two views, one with a button and another with a listbox. (each in their own prism region).
By clicking the button I create a new Employee object and insert it to the database via the entity framework.

The problem is that my listbox doesn’t show me the newly added Employee object in the list. What should be changed to make this work?

Some parts of the code:

MainView:

    <Grid>
    <StackPanel>
        <TextBlock Text="Hello From MainView (Core Module)" />
        <ListBox ItemsSource="{Binding Employees, Mode=TwoWay}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding FirstName}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </StackPanel>
</Grid>

MainViewModel:

namespace EmployeeViewer.Core.ViewModels
{
  [Export]
  [PartCreationPolicy(CreationPolicy.NonShared)]
  public class MainViewModel : NotificationObject
  {
    private IUnitOfWork _UnitOfWork;

[ImportingConstructor]
public MainViewModel(IUnitOfWork unitOfWork) // IEventAggregator eventAggregator
{
  this._UnitOfWork = unitOfWork;

  Init();
}

private void Init()
{
  this.Employees = new ObservableCollection<Employee>(_UnitOfWork.EmployeeRepository.Get());
  //this.Employees = new BindingList<Employee>(_UnitOfWork.EmployeeRepository.Get().ToList());
}

public ObservableCollection<Employee> Employees { get; set; }
//public BindingList<Employee> Employees { get; set; }

}
}

Get method from GenericRepository:

    public virtual IEnumerable<TEntity> Get(
  Expression<Func<TEntity, bool>> filter = null,
  Func<IQueryable<TEntity>,
  IOrderedQueryable<TEntity>> orderBy = null,
  string includeProperties = "")
{
  IQueryable<TEntity> query = _DbSet;

  if (filter != null)
  {
    query = query.Where(filter);
  }

  foreach (var includeProperty in includeProperties.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
  {
    query = query.Include(includeProperty);
  }

  if (orderBy != null)
  {
    return orderBy(query).AsEnumerable();//.ToList();
  }
  else
  {
    return query.AsEnumerable();//.ToList();
  }
}

Via an ICommand (implemented as RelayCommand) in another region and view, I create and insert a new Employee object to the Entity Framework code first.

public ICommand AddEmployeeCommand
{
  get { return _AddEmployeeCommand; }
}

private void AddEmployee()
{
  _UnitOfWork.EmployeeRepository.Insert(new Employee() { FirstName = "Test", LastName = "Via cmd" });
  _UnitOfWork.Save();
}
  • 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-25T19:51:02+00:00Added an answer on May 25, 2026 at 7:51 pm

    After searching on Bing.com I found out that EF has a Local property.

    In case you are looking for a solution here is what I did:

    I added a new Method to my GenericRepository:

    public virtual ObservableCollection<TEntity> GetSyncedEntityItems()
    {
      return _DbSet.Local;
    }
    

    This is the most important part: .Local gives you an ObservableCollection which is in sync with the EF!.

    In my ViewModel I do this:

    this.Employees = _UnitOfWork.EmployeeRepository.GetSyncedEntityItems();
    

    And adding a new Employee to the EF will cause my other View to update:

    private void AddEmployee()
    {
      _UnitOfWork.EmployeeRepository.Insert(new Employee() { FirstName = "Test", LastName = "Via cmd" });
      _UnitOfWork.Save();
    }
    

    I don’t know if this is the best solution, if there are better options, please do let me know!

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

Sidebar

Related Questions

I would really appreciate if somebody could tell me why compilation of this program:
That's an odd title. I would greatly appreciate it if somebody could clarify what
I would appreciate if somebody could help me with this (and explaining what's going
I would really appreciate if somebody could help me. I am trying to dynamically
Would appreciate if somebody could help me with something like this: NOTE!: I Do
I would really appreciate it, if somebody could help me out with my regular
I would really appreciate some assistance if somebody could help me construct a MSSQL
Could somebody please tell me what is Memory Page Out Rate. I have seen
Ok, if somebody could take a look at this, I'd really appreciate it. I'm
It would be great if somebody could help me with strategic input, how to

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.