I am attempting to implement a WPF ViewModel using Castle Windsor Dynamic Proxies. The idea is that I want to supply an interface (IPerson below should suffice as an example), a concrete backing class, and an interceptor (for providing automatic implementation of INotifyPropertyChanged). The interceptor implementation is here: http://www.hightech.ir/SeeSharp/Best-Implementation-Of-INotifyPropertyChange-Ever
The problem that I am seeing is that when I bind my models to WPF controls, the controls don't see the models as implementing INotifyPropertyChanged. I believe (but am not sure) that this is because Windsor is implementing the interfaces explicitly, and WPF seems to expect them to be implicit.
Is there any way to make this work, so that changes to the model are caught by the interceptor and raised to the model?
All versions of the libraries are the latest: Castle.Core 2.5.1.0 and Windsor 2.5.1.0
Code is as follows:
// My model's interface
public interface IPerson : INotifyPropertyChanged
{
string First { get; set; }
string LastName { get; set; }
DateTime Birthdate { get; set; }
}
// My concrete class:
[Interceptor(typeof(NotifyPropertyChangedInterceptor))]
class Person : IPerson
{
public event PropertyChangedEventHandler PropertyChanged = (s,e)=> { };
public string First { get; set; }
public string LastName { get; set; }
public DateTime Birthdate { get; set; }
}
// My windsor installer
public class Installer : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(
Component.For<NotifyPropertyChangedInterceptor>()
.ImplementedBy<NotifyPropertyChangedInterceptor>()
.LifeStyle.Transient);
container.Register(
Component.For<IPerson, INotifyPropertyChanged>()
.ImplementedBy<Person>().LifeStyle.Transient);
}
}
So the answer turned out to be fairly straightforward... The code from http://www.hightech.ir/SeeSharp/Best-Implementation-Of-INotifyPropertyChange-Ever defines the interceptor as:
public class NotifyPropertyChangedInterceptor : IInterceptor
{
private PropertyChangedEventHandler _subscribers = delegate { };
public void Intercept(IInvocation invocation)
{
if (invocation.Method.DeclaringType == typeof(INotifyPropertyChanged))
{
HandleSubscription(invocation);
return;
}
invocation.Proceed();
if (invocation.Method.Name.StartsWith("set_"))
{
FireNotificationChanged(invocation);
}
}
private void HandleSubscription(IInvocation invocation)
{
var handler = (PropertyChangedEventHandler)invocation.Arguments[0];
if (invocation.Method.Name.StartsWith("add_"))
{
_subscribers += handler;
}
else
{
_subscribers -= handler;
}
}
private void FireNotificationChanged(IInvocation invocation)
{
var propertyName = invocation.Method.Name.Substring(4);
_subscribers(invocation.InvocationTarget, new PropertyChangedEventArgs(propertyName));
}
}
In my case, the InvocationTarget was simply not the right entity to be passing as the first argument to PropertyChanged (because I am generating a proxy). Changing the last function to the following fixed the problem:
private void FireNotificationChanged(IInvocation invocation)
{
var propertyName = invocation.Method.Name.Substring(4);
_subscribers(invocation.Proxy, new PropertyChangedEventArgs(propertyName));
}
I think you need to make the members of your Class that implements the interface Virtual.
Related
My VS2015 solution consists of two projects: DataModel and DesktopClient.
DataModel has a Customer class - thats an EntityFramework 6 DB entity. Customer has a FirstName property.
In DesktopClient there is an extended class CustomerExt.
In DesktopClient, is it possible to have a notification to CustomerExt.FirstName changes? Defining a partial Customer across two projects won't work - DataModel is compiled first and it won't have partial properties defined in DesktopClient.
public class CustomerExt : Customer, INotifyPropertyChanged
{
public object Clone()
{
return this.MemberwiseClone();
}
private bool _isChecked;
public bool IsChecked
{
get { return _isChecked; }
set
{
this._isChecked = value;
NotifyPropertyChanged("IsChecked");
}
}
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(info));
}
}
Unfortunately, if your base class does not implement INotifyPropertyChanged the safest way is to just write a wrapper class and only use that in your software. You can fit this in with your CustExt, or make it separate if you feel you want the extra layer.
This also assumes that while you may not control the Customer class, you control all of the code creating/editing the Customer instances, so that you can use this new class instead, then convert it to the original Customer class only when needed (such as a database transaction).
public class CustomerExt: INotifyPropertyChanged
{
Customer _customer = new Customer();
public object Clone()
{
return this.MemberwiseClone();
}
private bool _isChecked;
public bool IsChecked
{
get { return _isChecked; }
set
{
this._isChecked = value;
NotifyPropertyChanged("IsChecked");
}
}
#region WrapperProperties
public bool FirstName
{
get { return _customer.FirstName; }
set
{
_customer.FirstName= value;
NotifyPropertyChanged("FirstName");
}
}
#endregion
public Customer ToCustomer()
{
// returning a copy of the _customer instance here is safer than returning
// the reference, otherwise the properties could be altered directly
}
#region INotifyPropertyChanged
...
}
Some of this gets a little easier if you have an ICustomer interface and that is used during the database calls, then you can skip the formality of retaining a Customer instance.
I remember there being some third party libraries that have tried to automate this process - but I have never tried them and/or didn't trust them to work properly.
Let me see if I understand, you want update the View when your date is updated on the database?
You have to find a way to request this information from your ViewModel.
some kind of RefreshFirstNameAsync
private string _firstName;
public string FirstName
{
get { return _firstName; }
set
{
this._firstName= value;
NotifyPropertyChanged("FirstName"); // There is better ways to implement that line
}
}
private void RefreshFirstName(){
FirstName = _userRepo.GetFirstNameAsync();
}
WPF-MVVM beginner here.
My problem: in a WPF-MVVM UI I am editing an entity. Some properties when changed, require automatic updates on other properties. These are done in Entity class, set methods, but not reflected in my View
More details:
1) I have the Model (a simple class with properties) in a separate assembly (not WPF related since is the general business model). Note that "SomeOption" when set to false, requires some other options to automatically be changed.
Example:
public class Employee : BaseEntity
{
public string EmployeeNumber { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
....
private bool someOption
public bool SomeOption {
get
{ return someOption}
set {
someOption= value;
if (!value)
{
OtherOption = false;
OtherProperty= "";
AndAnotherOption= false;
}
}
}
}
2) The WPF UI has a base ViewModel implementing INotifyPropertyChanged. The current edited record (Employee) is a public property of the ViewModel:
public Employee SelectedEmployee
{
get { return _selectedEmployee; }
set
{
if (_selectedEmployee != value)
{
_selectedEmployee = value;
OnPropertyChanged(nameof(SelectedEmployee));
}
}
}
3) When un-checking the checkbox bound to "SomeOption", the other properties which are changed in entity code, are not reflected on the View, and stay on the screen as edited by user.
Please let me know what I am missing. Thanks!
You should implement INotifyPropertyChanged in your model to update entities at your UI. For example:
public class Employee : BaseEntity, INotifyPropertyChanged
{
private string employeeNumber;
public string EmployeeNumber {
get{return employeeNumber};
set
{
employeeNumber=value;
OnPropertyChanged("EmployeeNumber");
}
//...Other properties...
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChangedEvent(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
Employee needs to implement INotifyPropertyChanged just as your viewmodel does, and fire PropertyChanged on changes to its own properties (the ones you're calling OtherOption, OtherProperty, etc.)
What you've got now will update the UI when the view model selects a different Employee, but subsequent changes to that Employee don't send any notifications.
Information for the question:
I am trying to understand how to properly implement INotifyPropertyChanged on objects and collections.
First, here is my ViewModelBase class:
public abstract class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertychanged([CallerMemberName] string propertyName = "")
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Consider that I have a class called Person:
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public string Age { get; set; }
}
To use INotifyPropertyChanged, most examples that I have seen change the Person class to something like this:
public class Person
{
public int Id { get; set; }
private string _name;
public string Name
{
get { return _name; }
set
{
_name = value;
OnPropertychanged();
}
}
private string _age;
public string Age
{
get { return _age; }
set
{
_age = value;
OnPropertychanged();
}
}
}
It seems to work exactly the same when used a single time on an instance of the object (This might be useful if there are a lot of properties):
private Person _person;
public Person MyPerson
{
get { return _person; }
set
{
_person = value;
OnPropertychanged();
}
}
Actual question:
1 - Does it make a difference (aside from amounts of code) whether you call OnPropertychanged() on each individual property verses on an instance of an object? (Are both considered good practice?)
2 - If setting OnPropertychanged() on the object instance is good practice, am I correct to create an ObservableCollection like this?:
var PersonCollection = new ObservableCollection<MyPerson>();
1) Well, if you want to call it on object instance, then you need to do it every time you use your class like this in binding. When you implement OnNotifyPropertyChanged directly inside your class, you don't need to care about it later on...
2) Classes with INotifyPropertyChanged do not require Observable collections. This is however must when you are binding colection do some UI control (ListBox, ListView) and want to add/remove its elements. Observable collection will then make sure the UI gets updated.
The ObservableCollections object... When adding and removing from this collection the UI will be notified of the changes (Top Level). If you have an "ObservableCollection of Person" and you change a property on the one of the objects(Person) in the list the UI will not update unless your "Person" class implements the INotifyPropertyChanged interface, which can be put into a base class that all classes can inherit from like your example. I hope this helps a little.
I have litle problem with PostSharp Implementation of INotifyPropertyChanged. PostSharp added PropertyChangedEventHandler PropertyChanged after compile time, but I need react from C# too.
Model a = new Model();
a.PropertyChanged += a_PropertyChanged;
Model implementation;
[NotifyPropertyChanged]
internal class Model
{
public string A { get; set; }
public string B { get; set; }
public string C { get { return string.Format("{0} - {1}", A, B); } }
}
I tried different ways to add handler,but unsuccessfully. Is there some way to do this?
Instance of class decorated by NotifyPropertyChanged can be casted to INotifyPropertyChanged at runtime:
((INotifyPropertyChanged)a).PropertyChanged
There is a helper method Post.Cast to avoid "Suspicious cast" warning:
Post.Cast<Model, INotifyPropertyChanged>(a).PropertyChanged += OnPropertyChanged;
I am getting started with WPF and MVVM. I am just reading up on Code Project Article which is quite interesting and provides a good quick start. I am using Entity Framework and I am happy I have listed all of my entities in a ListView.
I am curious how you would correctly implement lookups - in the view model or create a new model. Take the simple case of a person. The data structure might be:
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime DateOfBirth { get; set; }
public Toy FaveToy { get; set; }
}
public class Toy
{
public string Name { get; set; }
public string Model { get; set; }
public string Manufacturer { get; set; }
}
I want my list view to show the columns FirstName, LastName, DateOfBirth, Fave Toy, Manufacturer.
The Toy field will be a combined string with Name + " " + Model in.
So given some of the code in the example I've linked (I've knocked the Person class up for examples sake):
Base Class for the views
public abstract class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
My implementation for the Person view
public class PersonViewModel : ViewModelBase
{
private ObservableCollection<Person> _Person;
public PersonViewModel()
{
_Person = new ObservableCollection<Person>();
Entities context = new Entities();
foreach(var person in context.Person.ToList())
{
_Person.Add(person);
}
}
public ObservableCollection<Person> Person
{
get
{
return _Person;
}
set
{
_Person = value;
this.OnPropertyChanged("Person");
}
}
}
If I am understanding correct you want to access a Toy through the FaveToy property automatically.
Normally this would be done creating a relation between Person and Toy. Once you've done so your Person entity type would look like this:
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime DateOfBirth { get; set; }
public virtual Toy FaveToy { get; set; }
}
Notice the "virtual" on FaveToy. This indicates a navigation property which would be accessible through person instance.FaveToy. This can be represented in XAML either through dot notation or using the path property on your binding. Here is a link that discusses navigation properties: http://msdn.microsoft.com/en-us/data/jj713564.aspx
If you want to represent the toy by a combination of name and model then a converter would do the trick on the binding. Here is a link that discusses converters http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.aspx
Hope this helps...Jason
PS - Josh Smith is awesome in his discussion of WPF and MVVM http://joshsmithonwpf.wordpress.com/a-guided-tour-of-wpf/
If you're a VB guy Alessandro has trhe best discussion for VB that I've seen: http://community.visual-basic.it/Alessandroenglish/archive/2010/08/13/31861.aspx
PSS -- WPF and MVVM (or Windows 8 and XAML) are awesome. Definitely worth your time to pick up.