0

This is my first question not in stackoverflow only but the whole in internet. I have a wpf project with Entity Framework 6 EF6 model auto-generated from Sql server database. I have the following class:

public partial class SA_EMPLOYEE
{
    public int EMPID { get; set; }
    public string FIRST_NAME { get; set; }
    public string LAST_NAME { get; set; }
}

//then I added a custom property
public partial class SA_EMPLOYEE
{
    public string FULL_NAME => $"{FIRST_NAME} {LAST_NAME}";
}

I have a list box binding to myDbContext.SA_EMPLOYEE.ToList() and is showing the FULL_NAME property and some controls binding to selected item.

When I show the FIRST_NAME in the listbox it changed immediately if I change it in the textbox but not for FULL_NAME. Any work around to notify property changed for FULL_NAME if any of FIRST_NAME or LAST_NAME has changed.

I saw a similar question here.

Thanks for anyone could help.

Mithgroth
  • 1,114
  • 2
  • 13
  • 23
iyado
  • 1
  • 2
  • 1
    I don't get the point completely. My suggestion is to watch over `INotifyPropertyChanged` interface. You can overwrite the default bevavior of EF6 models by customizing the T4 template. Avoiding direct of binding of EF models and using transport models instead would be even better. Maybe you should take a look to the MVVM pattern. – Alexander Schmidt Dec 29 '17 at 12:26

2 Answers2

0

Your solution is an INotifyPropertyChanged interface.From this link, you will find the exact solution to your question.

RASKOLNIKOV
  • 732
  • 2
  • 9
  • 20
  • Thank you but this is not my case, I'm using entity framework 6 which is already implement notify property. but not for my extended property – iyado Dec 29 '17 at 13:14
0

From your description, I think you probably should use the MVVM pattern (as suggested above). Create a ViewModel and bind to that instead of the database model.

public class EmployeeViewModel : INotifyPropertyChanged
{
  public event PropertyChangedEventHandler PropertyChanged;

  public string FirstName {get;set;}
  public string LastName {get;set;}
  public string FullName => $"{FirstName} {LastName}";
}

If you use Fody/PropertyChanged you will get the PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("FullName")); call automatic, otherwise you have to call OnPropertyChanged manually in the set on FirstName and LastName properties. Se the link above for an example.

You then bind the listbox to the IObservableCollection of employees.

You might want to look into an MVVM-framework like Prism or ReactiveUI

As a side note, I think FamilyName and GivenName are better than FirstName and LastName, considering for example countries like Korea where the family name is the first name.

Kricke242
  • 59
  • 4