finding selected items in Telerik RadGridView using MVVM pattern - mvvm

I am using a Telerik RadGridView in my application and it has a GridViewSelectColumn item in it, which allows me to select various items in the grid. I have a button that operates on this selection, but am not sure how to get the list of selected items. The problem is that I am using an MVVM pattern with Caliburn.Micro. Do I need to find the control in the view and traverse the list of selected items? That seems like a lot of work for a simple task. I would appreciate any ideas.

The problem with Telerik's RadGridView is, that its SelectedItem collection is read-only, so you cannot bind two-way to SelectedItems.
A workaround for this is to use a custom Behavior to do the synchronization between RadGridView and your ViewModels SelectedItem collection
You may use this Behavior:
// Behavior for synchronizing a RadDataGrid's SelectedItems collection with a SelectedItems collection of the ViewModel (the Network)
// The problem is, that RadDataGrid.SelectedItems is a read-only collection and therefore cannot be used for two-way binding.
class SelectedSyncBehavior
: Behavior<RadGridView>
{
bool _collectionChangedSuspended = false;
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.SelectedItems.CollectionChanged += GridSelectedItems_CollectionChanged;
}
/// <summary>
/// Getter/Setter for DependencyProperty, bound to the DataContext's SelectedItems ObservableCollection
/// </summary>
public INotifyCollectionChanged SelectedItems
{
get { return (INotifyCollectionChanged)GetValue(SelectedItemsProperty); }
set { SetValue(SelectedItemsProperty, value); }
}
/// <summary>
/// Dependency Property "SelectedItems" is target of binding to DataContext's SelectedItems
/// </summary>
public static readonly DependencyProperty SelectedItemsProperty =
DependencyProperty.Register("SelectedItems", typeof(INotifyCollectionChanged), typeof(SelectedSyncBehavior), new PropertyMetadata(OnSelectedItemsPropertyChanged));
/// <summary>
/// PropertyChanged handler for DependencyProperty "SelectedItems"
/// </summary>
private static void OnSelectedItemsPropertyChanged(DependencyObject target, DependencyPropertyChangedEventArgs args)
{
INotifyCollectionChanged collection = args.NewValue as INotifyCollectionChanged;
if (collection != null)
{
// Hook to the Network's SelectedItems
collection.CollectionChanged += (target as SelectedSyncBehavior).ContextSelectedItems_CollectionChanged;
}
}
/// <summary>
/// Will be called, when the Network's SelectedItems collection changes
/// </summary>
void ContextSelectedItems_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (_collectionChangedSuspended) return; // Don't react recursively to CollectionChanged events
_collectionChangedSuspended = true;
// Select and unselect items in the grid
if (e.NewItems != null)
foreach (object item in e.NewItems)
AssociatedObject.SelectedItems.Add(item);
if (e.OldItems != null)
foreach (object item in e.OldItems)
AssociatedObject.SelectedItems.Remove(item);
_collectionChangedSuspended = false;
}
/// <summary>
/// Will be called when the GridView's SelectedItems collection changes
/// </summary>
void GridSelectedItems_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (_collectionChangedSuspended) return; // Don't react recursively to CollectionChanged events
_collectionChangedSuspended = true;
// Select and unselect items in the DataContext
if (e.NewItems != null)
foreach (object item in e.NewItems)
(SelectedItems as IList).Add(item);
if (e.OldItems != null)
foreach (object item in e.OldItems)
(SelectedItems as IList).Remove(item);
_collectionChangedSuspended = false;
}
}
Use this Behavior with RadGridViews like this:
<i:Interaction.Behaviors>
<behaviors:SelectedSyncBehavior SelectedItems="{Binding ViewModel.SelectedItems}" />
</i:Interaction.Behaviors>

Add a bool IsSelected to the item in your collection:
public class Customer
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public bool IsSelected { get; set; }
}
private BindableCollection<Customer> _customers;
public BindableCollection<Customer> Customers
{
get { return _customers; }
set
{
_customers = value;
NotifyOfPropertyChange(() => Customers);
}
}
sample code - bitbucket
download

Here is a cleaned up copy of #Knasterbax's class with explicit private modifiers and null propagation:
// Behavior for synchronizing Telerik RadDataGrid's SelectedItems collection
// with a SelectedItems collection of the ViewModel.
public class SelectedSyncBehavior : Behavior<RadGridView>
{
private bool collectionChangedSuspended;
public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.Register("SelectedItems",
typeof(INotifyCollectionChanged), typeof(SelectedSyncBehavior), new PropertyMetadata(OnSelectedItemsPropertyChanged));
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.SelectedItems.CollectionChanged += GridSelectedItems_CollectionChanged;
}
public INotifyCollectionChanged SelectedItems
{
get { return (INotifyCollectionChanged)GetValue(SelectedItemsProperty); }
set { SetValue(SelectedItemsProperty, value); }
}
private static void OnSelectedItemsPropertyChanged(DependencyObject target, DependencyPropertyChangedEventArgs args)
{
var collection = args.NewValue as INotifyCollectionChanged;
if (collection == null) return;
var selectedSyncBehavior = target as SelectedSyncBehavior;
if (selectedSyncBehavior != null) collection.CollectionChanged += selectedSyncBehavior.ContextSelectedItems_CollectionChanged;
}
private void ContextSelectedItems_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (collectionChangedSuspended) return; // Don't react recursively to CollectionChanged events
collectionChangedSuspended = true;
if (e.NewItems != null)
foreach (var item in e.NewItems)
AssociatedObject.SelectedItems.Add(item);
if (e.OldItems != null)
foreach (var item in e.OldItems)
AssociatedObject.SelectedItems.Remove(item);
collectionChangedSuspended = false;
}
private void GridSelectedItems_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (collectionChangedSuspended) return; // Don't react recursively to CollectionChanged events
collectionChangedSuspended = true;
if (e.NewItems != null)
foreach (var item in e.NewItems)
{
var list = SelectedItems as IList;
list?.Add(item);
}
if (e.OldItems != null)
foreach (var item in e.OldItems)
{
var list = SelectedItems as IList;
list?.Remove(item);
}
collectionChangedSuspended = false;
}
}

Here's a cleanup version of the answer above. This removes underscores, adds qualifiers, accessors, braces, etc.
public class SelectedItemsBehavior : Behavior<RadGridView>
{
private bool collectionChangedSuspended;
/// <summary>
/// Called after the behavior is attached to an AssociatedObject.
/// </summary>
/// <remarks>
/// Override this to hook up functionality to the AssociatedObject.
/// </remarks>
protected override void OnAttached()
{
base.OnAttached();
this.AssociatedObject.SelectedItems.CollectionChanged += this.GridSelectedItemsCollectionChanged;
}
/// <summary>
/// Getter/Setter for DependencyProperty, bound to the DataContext's SelectedItems ObservableCollection
/// </summary>
public INotifyCollectionChanged SelectedItems
{
get => (INotifyCollectionChanged)this.GetValue(SelectedItemsProperty);
set => this.SetValue(SelectedItemsProperty, value);
}
/// <summary>
/// Dependency Property "SelectedItems" is target of binding to DataContext's SelectedItems
/// </summary>
public static readonly DependencyProperty SelectedItemsProperty =
DependencyProperty.Register("SelectedItems", typeof(INotifyCollectionChanged), typeof(SelectedItemsBehavior), new PropertyMetadata(OnSelectedItemsPropertyChanged));
/// <summary>
/// PropertyChanged handler for DependencyProperty "SelectedItems"
/// </summary>
private static void OnSelectedItemsPropertyChanged(DependencyObject target, DependencyPropertyChangedEventArgs args)
{
INotifyCollectionChanged collection = args.NewValue as INotifyCollectionChanged;
if (collection != null)
{
collection.CollectionChanged += ((SelectedItemsBehavior)target).ContextSelectedItemsCollectionChanged;
}
}
/// <summary>
/// Will be called, when the Network's SelectedItems collection changes
/// </summary>
private void ContextSelectedItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (this.collectionChangedSuspended)
{
return;
}
this.collectionChangedSuspended = true;
if (e.NewItems != null)
{
foreach (object item in e.NewItems)
{
this.AssociatedObject.SelectedItems.Add(item);
}
}
if (e.OldItems != null)
{
foreach (object item in e.OldItems)
{
this.AssociatedObject.SelectedItems.Remove(item);
}
}
this.collectionChangedSuspended = false;
}
/// <summary>
/// Will be called when the GridView's SelectedItems collection changes
/// </summary>
private void GridSelectedItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (this.collectionChangedSuspended)
{
return;
}
this.collectionChangedSuspended = true;
if (e.NewItems != null)
{
foreach (object item in e.NewItems)
{
((IList)this.SelectedItems).Add(item);
}
}
if (e.OldItems != null)
{
foreach (object item in e.OldItems)
{
((IList)this.SelectedItems).Remove(item);
}
}
this.collectionChangedSuspended = false;
}
}

There are a situation where you cannot add boolean (ObseravbleCollection for example.
Please take a look at this solution.

Related

Best way to handle complex entities (relational) with Generic CRUD functions

I have tried using this generic functions to insert-update Entities but I always thought that maybe I am doing this totally wrong so therefore I would like to have your opinions/suggestions.
These are my Insert & Update functions:
public static bool Insert<T>(T item) where T : class
{
using (ApplicationDbContext ctx = new ApplicationDbContext())
{
try
{
ctx.Set<T>().Add(item);
ctx.SaveChanges();
return true;
}
catch (Exception ex)
{
// ...
}
}
}
public static bool Update<T>(T item) where T : class
{
using (ApplicationDbContext ctx = new ApplicationDbContext())
{
try
{
Type itemType = item.GetType();
// switch statement to perform actions according which type we are working on
ctx.SaveChanges();
return true;
}
catch (Exception ex)
{
// ...
}
}
}
I have learned that i can use ctx.Entry(item).State = EntityState.Modified; and I have seen so many ways of inserting-updating entities that I am very curious on what is the easiest most manageable way of performing CRUD actions ?
I know about the repository pattern and so on but i don't have much experience with interfaces or I don't seem to fully understand whats used so I prefer not to use it till I fully get it.
my approach for that is to use IRepository pattern to wrap CRUD and to make dependencies injection easier in my application, here an example on how i do it:
Define your contract like following:
(i am simplifying the example and admitting that all your tables have an integer id -i mean it is not guid or string or whatever- )
public interface IGenericRepository<TEntity> where TEntity : class
{
#region ReadOnlyRepository
TEntity GetById(int id);
ICollection<TEntity> GetAll();
ICollection<TEntity> GetAll(params Expression<Func<TEntity, object>>[] includeProperties);
ICollection<TEntity> Query(Expression<Func<TEntity, bool>> expression, params Expression<Func<TEntity, object>>[] includeProperties);
PagedModel<TEntity> Query(Expression<Func<TEntity, bool>> expression, SortOptions sortOptions, PaginateOptions paginateOptions, params Expression<Func<TEntity, object>>[] includeProperties);
int Max(Expression<Func<TEntity, int>> expression);
#endregion
#region PersistRepository
bool Add(TEntity entity);
bool AddRange(IEnumerable<TEntity> items);
bool Update(TEntity entity);
bool Delete(TEntity entity);
bool DeleteById(int id);
#endregion
}
and then the implementation:
public class GenericRepository<TEntity> : IGenericRepository<TEntity> where TEntity : class
{
#region Fields
protected DbContext CurrentContext { get; private set; }
protected DbSet<TEntity> EntitySet { get; private set; }
#endregion
#region Ctor
public GenericRepository(DbContext context)
{
CurrentContext = context;
EntitySet = CurrentContext.Set<TEntity>();
}
#endregion
#region IReadOnlyRepository Implementation
public virtual TEntity GetById(int id)
{
try
{
//use your logging method (log 4 net used here)
DomainEventSource.Log.Info(string.Format("getting entity {0} with id {1}", typeof(TEntity).Name, id));
return EntitySet.Find(id); //dbcontext manipulation
}
catch (Exception exception)
{
/// example of error handling
DomainEventSource.Log.Error(exception.Message);
var errors = new List<ExceptionDetail> { new ExceptionDetail { ErrorMessage = exception.Message } };
throw new ServerException(errors);// this is specific error formatting class you can do somthing like that to fit your needs
}
}
public virtual ICollection<TEntity> GetAll()
{
try
{
return EntitySet.ToList();
}
catch (Exception exception)
{
//... Do whatever you want
}
}
public virtual ICollection<TEntity> GetAll(params Expression<Func<TEntity, object>>[] includeProperties)
{
try
{
var query = LoadProperties(includeProperties);
return query.ToList();
}
catch (Exception exception)
{
//... Do whatever you want
}
}
public virtual ICollection<TEntity> Query(Expression<Func<TEntity, bool>> expression, params Expression<Func<TEntity, object>>[] includeProperties)
{
try
{
var query = LoadProperties(includeProperties);
return query.Where(expression).ToList();
}
catch (Exception exception)
{
//... Do whatever you want
}
}
// returning paged results for example
public PagedModel<TEntity> Query(Expression<Func<TEntity, bool>> expression,SortOptions sortOptions, PaginateOptions paginateOptions, params Expression<Func<TEntity, object>>[] includeProperties)
{
try
{
var query = EntitySet.AsQueryable().Where(expression);
var count = query.Count();
//Unfortunatly includes can't be covered with a UT and Mocked DbSets...
if (includeProperties.Length != 0)
query = includeProperties.Aggregate(query, (current, prop) => current.Include(prop));
if (paginateOptions == null || paginateOptions.PageSize <= 0 || paginateOptions.CurrentPage <= 0)
return new PagedModel<TEntity> // specific pagination model, you can define yours
{
Results = query.ToList(),
TotalNumberOfRecords = count
};
if (sortOptions != null)
query = query.OrderByPropertyOrField(sortOptions.OrderByProperty, sortOptions.IsAscending);
var skipAmount = paginateOptions.PageSize * (paginateOptions.CurrentPage - 1);
query = query.Skip(skipAmount).Take(paginateOptions.PageSize);
return new PagedModel<TEntity>
{
Results = query.ToList(),
TotalNumberOfRecords = count,
CurrentPage = paginateOptions.CurrentPage,
TotalNumberOfPages = (count / paginateOptions.PageSize) + (count % paginateOptions.PageSize == 0 ? 0 : 1)
};
}
catch (Exception exception)
{
var errors = new List<ExceptionDetail> { new ExceptionDetail { ErrorMessage = exception.Message } };
throw new ServerException(errors);
}
}
#endregion
#region IPersistRepository Repository
public bool Add(TEntity entity)
{
try
{
// you can do some extention methods here to set up creation date when inserting or createdBy etc...
EntitySet.Add(entity);
return true;
}
catch (Exception exception)
{
//DomainEventSource.Log.Failure(ex.Message);
//or
var errors = new List<ExceptionDetail> { new ExceptionDetail { ErrorMessage = exception.Message } };
throw new ServerException(errors);
}
}
public bool AddRange(IEnumerable<TEntity> items)
{
try
{
foreach (var entity in items)
{
Add(entity);
}
}
catch (Exception exception)
{
//DomainEventSource.Log.Failure(ex.Message);
var errors = new List<ExceptionDetail> { new ExceptionDetail { ErrorMessage = exception.Message } };
throw new ServerException(errors);
}
return true;
}
public bool Update(TEntity entity)
{
try
{
CurrentContext.Entry(entity).State = EntityState.Modified;
}
catch (Exception exception)
{
var errors = new List<ExceptionDetail> { new ExceptionDetail { ErrorMessage = exception.Message } };
throw new ServerException(errors);
}
return true;
}
public bool Delete(TEntity entity)
{
try
{
if (CurrentContext.Entry(entity).State == EntityState.Detached)
{
EntitySet.Attach(entity);
}
EntitySet.Remove(entity);
}
catch (Exception exception)
{
var errors = new List<ExceptionDetail> { new ExceptionDetail { ErrorMessage = exception.Message } };
throw new ServerException(errors);
}
return true;
}
public bool DeleteById(TKey id)
{
var entityToDelete = GetById(id);
return Delete(entityToDelete);
}
#endregion
#region Loading dependancies Utilities
private IQueryable<TEntity> LoadProperties(IEnumerable<Expression<Func<TEntity, object>>> includeProperties)
{
return includeProperties.Aggregate<Expression<Func<TEntity, object>>, IQueryable<TEntity>>(EntitySet, (current, includeProperty) => current.Include(includeProperty));
}
#endregion
}
I am admitting that your model classes are already created and decorated.
After this , you need to create your entityRepository like following : this is an example of managing entity called Ticket.cs
public class TicketRepository : GenericRepository<Ticket>, ITicketRepository
{
// the EntityRepository classes are made in case you have some ticket specific methods that doesn't
//have to be in generic repository
public TicketRepository(DbContext context)
: base(context)
{
}
// Add specific generic ticket methods here (not business methods-business methods will come later-)
}
After this comes the UnitOfWork class which allows us to unify entry to the database context and provides us an instance of repositories on demand using dependency injection
public class UnitOfwork : IUnitOfWork
{
#region Fields
protected DbContext CurrentContext { get; private set; }
private ITicketRepository _tickets;
#endregion
#region ctor
public UnitOfwork(DbContext context)
{
CurrentContext = context;
}
#endregion
#region UnitOfWorkBaseImplementation
public void Commit()
{
try
{
CurrentContext.SaveChanges();
}
catch (Exception e)
{
/// catch
}
}
public void Rollback()
{
foreach (var entry in CurrentContext.ChangeTracker.Entries())
{
switch (entry.State)
{
case EntityState.Modified:
case EntityState.Deleted:
entry.State = EntityState.Modified; //Revert changes made to deleted entity.
entry.State = EntityState.Unchanged;
break;
case EntityState.Added:
entry.State = EntityState.Detached;
break;
case EntityState.Detached:
break;
case EntityState.Unchanged:
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
#region complete RollBack()
private void RejectScalarChanges()
{
foreach (var entry in CurrentContext.ChangeTracker.Entries())
{
switch (entry.State)
{
case EntityState.Modified:
case EntityState.Deleted:
entry.State = EntityState.Modified; //Revert changes made to deleted entity.
entry.State = EntityState.Unchanged;
break;
case EntityState.Added:
entry.State = EntityState.Detached;
break;
case EntityState.Detached:
break;
case EntityState.Unchanged:
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
private void RejectNavigationChanges()
{
var objectContext = ((IObjectContextAdapter)this).ObjectContext;
var deletedRelationships = objectContext.ObjectStateManager.GetObjectStateEntries(EntityState.Deleted).Where(e => e.IsRelationship && !this.RelationshipContainsKeyEntry(e));
var addedRelationships = objectContext.ObjectStateManager.GetObjectStateEntries(EntityState.Added).Where(e => e.IsRelationship);
foreach (var relationship in addedRelationships)
relationship.Delete();
foreach (var relationship in deletedRelationships)
relationship.ChangeState(EntityState.Unchanged);
}
private bool RelationshipContainsKeyEntry(System.Data.Entity.Core.Objects.ObjectStateEntry stateEntry)
{
//prevent exception: "Cannot change state of a relationship if one of the ends of the relationship is a KeyEntry"
//I haven't been able to find the conditions under which this happens, but it sometimes does.
var objectContext = ((IObjectContextAdapter)this).ObjectContext;
var keys = new[] { stateEntry.OriginalValues[0], stateEntry.OriginalValues[1] };
return keys.Any(key => objectContext.ObjectStateManager.GetObjectStateEntry(key).Entity == null);
}
#endregion
public void Dispose()
{
if (CurrentContext != null)
{
CurrentContext.Dispose();
}
}
#endregion
#region properties
public ITicketRepository Tickets
{
get { return _tickets ?? (_tickets = new TicketRepository(CurrentContext)); }
}
#endregion
}
Now for the last part we move to our business service layer and make a ServiceBase class which will be implemented by all business services
public class ServiceBase : IServiceBase
{
private bool _disposed;
#region IServiceBase Implementation
[Dependency]
public IUnitOfWork UnitOfWork { protected get; set; }
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (_disposed)
return;
if (disposing)
{
var disposableUow = UnitOfWork as IDisposable;
if (disposableUow != null)
disposableUow.Dispose();
}
_disposed = true;
}
#endregion
}
and finally one example of business service class and how to use your CRUD and play with your business rules (i am using properties injection which is not the best to do so i suggest to change it and use constructor injection instead)
public class TicketService : ServiceBase, ITicketService
{
#region fields
private IUserService _userService;
private IAuthorizationService _authorizationService;
#endregion
#region Properties
[Dependency]
public IAuthorizationService AuthorizationService
{
set { _authorizationService = value; }
}
[Dependency]
public IUserService UserService
{
set { _userService = value; }
}
public List<ExceptionDetail> Errors { get; set; }
#endregion
#region Ctor
public TicketService()
{
Errors = new List<ExceptionDetail>();
}
#endregion
#region IServiceBase Implementation
/// <summary>
/// desc
/// </summary>
/// <returns>array of TicketAnomalie</returns>
public ICollection<Ticket> GetAll()
{
return UnitOfWork.Tickets.GetAll();
}
/// <summary>
/// desc
/// </summary>
/// <param name="id"></param>
/// <returns>TicketAnomalie</returns>
public Ticket GetTicketById(int id)
{
return UnitOfWork.Tickets.GetById(id);
}
/// <summary>
/// description here
/// </summary>
/// <returns>Collection of Ticket</returns>
public ICollection<Ticket> GetAllTicketsWithDependencies()
{
return UnitOfWork.Tickets.Query(tick => true, tick => tick.Anomalies);
}
/// <summary>
/// description here
/// </summary>
/// <param name="id"></param>
/// <returns>Ticket</returns>
public Ticket GetTicketWithDependencies(int id)
{
return UnitOfWork.Tickets.Query(tick => tick.Id == id, tick => tick.Anomalies).SingleOrDefault();
}
/// <summary>
/// Add new ticket to DB
/// </summary>
/// <param name="anomalieId"></param>
/// <returns>Boolean</returns>
public bool Add(int anomalieId)
{
var anomalie = UnitOfWork.Anomalies.Query(ano => ano.Id.Equals(anomalieId), ano => ano.Tickets).FirstOrDefault();
var currentUser = WacContext.Current;
var superv = _userService.GetSupervisorUserProfile();
var sup = superv.FirstOrDefault();
if (anomalie != null)
{
var anomalies = new List<Anomalie>();
var anom = UnitOfWork.Anomalies.GetById(anomalieId);
anomalies.Add(anom);
if (anomalie.Tickets.Count == 0 && sup != null)
{
var ticket = new Ticket
{
User = sup.Id,
CreatedBy = currentUser.GivenName,
Anomalies = anomalies,
Path = UnitOfWork.SearchCriterias.GetById(anom.ParcoursId),
ContactPoint = UnitOfWork.ContactPoints.GetById(anom.ContactPointId)
};
UnitOfWork.Tickets.Add(ticket);
UnitOfWork.Commit();
}
}
else
{
Errors.Add(AnomaliesExceptions.AnoNullException);
}
if (Errors.Count != 0) throw new BusinessException(Errors);
return true;
}
public bool Update(Ticket ticket)
{
if (ticket == null)
{
Errors.Add(AnomaliesExceptions.AnoNullException);
}
else
if (!Exists(ticket.Id))
{
Errors.Add(AnomaliesExceptions.AnoToUpdateNotExistException);
}
if (Errors.Count != 0) throw new BusinessException(Errors);
UnitOfWork.Tickets.Update(ticket);
UnitOfWork.Commit();
return true;
}
public bool Exists(int ticketId)
{
var operationDbEntity =
UnitOfWork.Tickets.Query(t => t.Id.Equals(ticketId)).ToList();
return operationDbEntity.Count != 0;
}
#endregion
#region Business Implementation
//play with your buiness :)
#endregion
}
Finally,
i suggest that you redo this using asynchronous methods (async await since it allows a better management of service pools in the web server)
Note that this is my own way of managing my CRUD with EF and Unity. you can find a lot of other implementations that can inspire you.
Hope this helps,

Create Wizard with MVVM Pattern - Databinding and GUI Update not working

I am creating a Wizard for automatic Report generating. Therefore the user enters a couple of Issues to the Mainwindow and after he finished, he can create an automatically filled report by clicking a button. Also he need to switch between the entered Issuses (Therefore I put the issues to a list of the Type "Compliant".
The MainWindow contains different controls, in which the user can enter some information.
All controls are binded to the Model like this way (example for a textbox):
Text="{Binding Path=SingleCompliant.Text, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
"SingleCompliant" is an object of the ViewModel of the type "Compliant" (part of the Model), which contains all attributes needed for one Issue.
When I load the Application for the first time, everything works fine.
But when I click on "SaveCompliantAndNext", the binding between "SingleCompliant" Object gets lost and the GUI is not updating. In this Method (raised by a command) I create a new SingleCompliant Object.
What do I need to do for getting a new Object with an empty gui, so the user can continue entereing the next Issue?
This is the ViewModel with implemented "PropertyChanged" Handling:
public class Compliant_ViewModel : ObservableCollection<Compliant> //INotifyPropertyChanged
{
/// <summary>
/// MainWindow Controls are binded to Attributes of this object
/// </summary>
private Compliant _SingleCompliant;
public ObservableCollection<Compliant> CompliantListReport
{
get { return _CompliantListReport; }
private set { _CompliantListReport = value; }
}
/// <summary>
/// Beandstandungs-Objekt
/// </summary>
public Compliant SingleCompliant
{
get { return _SingleCompliant; }
set
{ _SingleCompliant = value;
OnPropertyChanged("SingleCompliant");
}
}
/// <summary>
/// Load next Compliant to the GUI
/// </summary>
public NextCompliant_Command NextCompliant_command { get; private set; }
/// <summary>
/// Load previous Compliant to the GUI
/// </summary>
public PreviousCompliant_Command PreviousCompliant_Command { get; private set; }
/// <summary>
/// Constructor
/// </summary>
public Compliant_ViewModel()
{
SingleCompliant = new Compliant();
CompliantListReport = new ObservableCollection<Compliant>();
NextCompliant_command = new NextCompliant_Command(SaveCompliantAndNext);
PreviousCompliant_Command = new PreviousCompliant_Command(LoadPreviousCompliant);
CreateReport_Command = new CreateReport_Command(CreateReport);
}
public void SaveCompliantAndNext()
{
CompliantListReport.Add(SingleCompliant);
// >>>>> Not working - databinding get lost and gui not updating
SingleCompliant = new Compliant();
}
public void LoadPreviousCompliant()
{
if (this.SingleCompliant.CompliantID > 0)
{ this.SingleCompliant = this.CompliantListReport[this.SingleCompliant.CompliantID - 1]; }
else
{ this.SingleCompliant = this.CompliantListReport[0]; }
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyname)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{ handler(this, new PropertyChangedEventArgs(propertyname)); }
}
}
This is the Model:
public class Compliant : INotifyPropertyChanged
{
public string _Text;
public string Text
{
get { return _Text; }
private set
{
_Text = value;
OnPropertyChanged("Text");
}
}
#region Konstruktoren
public Compliant()
{ }
#region Interface
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyname)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyname));
}
}
#endregion Interface
}
XAML:
Example for one Textbox:
<TextBox Text="{Binding Path=SingleCompliant.Text, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" x:Name="Text" />
XAML Buttons to switch between the Compliants (Issues):
<!--Previous Compliant (Issue)-->
<Button x:Name="BtnBack_Compliant" Content="{DynamicResource Back}" Command="{Binding PreviousCompliant_Command}"/>
<!--Next Compliant (Issue)-->
<Button x:Name="BtnForeward_Compliant" Content="{DynamicResource Foreward}" Command="{Binding NextCompliant_command}" />
What do I make wrong?

WPF MVVM Command CanExecute, reevaluates only at focus change

Refactoring an MVVM project in WPF, I'm trying to get rid of what seems a common problem between MVVM pattern users.
I have view, who's DataContext is MyViewModel. Here is a button, bound with a Command that implements both Execute and CanExecute.
XAML:
<Button Command="{Binding ConnectCommand}"/>
MyViewModel exposes ConnectCommand:
public ICommand ConnectCommand
{
get { return new DelegateCommand(() => Connect(), () => IsConnectEnabled); }
}
(at the end the definition of DelegateCommand I'm using)
MyViewModel also exposes the property IsConnectEnabled, used in the CanExecute part of the Command:
public bool IsConnectEnabled
{
get
{
return (isDisconnected && null!=selectedDevice && 0<selectedDevice.Length);
}
}
MyViewModel class implements the INotifyPropertyChanged interface
public class MyViewModel : INotifyPropertyChanged
{
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
#region rest of the class
}
The CanExecute part of the command is only evaluated on a change of focus in the application (ie, whatever click I do). I know that the UpdateSourceTrigger is by default set to PropertyChanged, therefore my current solution, is to manually raise a PropertyChanged event in a few places in the code. But I want to do better and have this activity done automatically whenever the value of IsConnectEnabled changes.
Does the WPF and the MVVM pattern offer a solution for this issue?
For completeness, follows the complete ICommand implementation I'm using, DelegateCommand:
/// <summary>
/// This class allows delegating the commanding logic to methods passed as parameters,
/// and enables a View to bind commands to objects that are not part of the element tree.
/// </summary>
public class DelegateCommand : ICommand
{
/// <summary>
/// Constructor
/// </summary>
public DelegateCommand(Action executeMethod)
: this(executeMethod, null, false)
{
}
/// <summary>
/// Constructor
/// </summary>
public DelegateCommand(Action executeMethod, Func<bool> canExecuteMethod)
: this(executeMethod, canExecuteMethod, false)
{
}
/// <summary>
/// Constructor
/// </summary>
public DelegateCommand(Action executeMethod, Func<bool> canExecuteMethod, bool isAutomaticRequeryDisabled)
{
if (executeMethod == null)
{
throw new ArgumentNullException("executeMethod");
}
_executeMethod = executeMethod;
_canExecuteMethod = canExecuteMethod;
_isAutomaticRequeryDisabled = isAutomaticRequeryDisabled;
}
#region Public Methods
/// <summary>
/// Method to determine if the command can be executed
/// </summary>
public bool CanExecute()
{
if (_canExecuteMethod != null)
{
return _canExecuteMethod();
}
return true;
}
/// <summary>
/// Execution of the command
/// </summary>
public void Execute()
{
if (_executeMethod != null)
{
_executeMethod();
}
}
/// <summary>
/// Property to enable or disable CommandManager's automatic requery on this command
/// </summary>
public bool IsAutomaticRequeryDisabled
{
get
{
return _isAutomaticRequeryDisabled;
}
set
{
if (_isAutomaticRequeryDisabled != value)
{
if (value)
{
CommandManagerHelper.RemoveHandlersFromRequerySuggested(_canExecuteChangedHandlers);
}
else
{
CommandManagerHelper.AddHandlersToRequerySuggested(_canExecuteChangedHandlers);
}
_isAutomaticRequeryDisabled = value;
}
}
}
/// <summary>
/// Raises the CanExecuteChaged event
/// </summary>
public void RaiseCanExecuteChanged()
{
OnCanExecuteChanged();
}
/// <summary>
/// Protected virtual method to raise CanExecuteChanged event
/// </summary>
protected virtual void OnCanExecuteChanged()
{
CommandManagerHelper.CallWeakReferenceHandlers(_canExecuteChangedHandlers);
}
#endregion
#region ICommand Members
/// <summary>
/// ICommand.CanExecuteChanged implementation
/// </summary>
public event EventHandler CanExecuteChanged
{
add
{
if (!_isAutomaticRequeryDisabled)
{
CommandManager.RequerySuggested += value;
}
CommandManagerHelper.AddWeakReferenceHandler(ref _canExecuteChangedHandlers, value, 2);
}
remove
{
if (!_isAutomaticRequeryDisabled)
{
CommandManager.RequerySuggested -= value;
}
CommandManagerHelper.RemoveWeakReferenceHandler(_canExecuteChangedHandlers, value);
}
}
bool ICommand.CanExecute(object parameter)
{
return CanExecute();
}
void ICommand.Execute(object parameter)
{
Execute();
}
#endregion
#region Data
private readonly Action _executeMethod = null;
private readonly Func<bool> _canExecuteMethod = null;
private bool _isAutomaticRequeryDisabled = false;
private List<WeakReference> _canExecuteChangedHandlers;
#endregion
}
/// <summary>
/// This class allows delegating the commanding logic to methods passed as parameters,
/// and enables a View to bind commands to objects that are not part of the element tree.
/// </summary>
/// <typeparam name="T">Type of the parameter passed to the delegates</typeparam>
public class DelegateCommand<T> : ICommand
{
#region Constructors
/// <summary>
/// Constructor
/// </summary>
public DelegateCommand(Action<T> executeMethod)
: this(executeMethod, null, false)
{
}
/// <summary>
/// Constructor
/// </summary>
public DelegateCommand(Action<T> executeMethod, Func<T, bool> canExecuteMethod)
: this(executeMethod, canExecuteMethod, false)
{
}
/// <summary>
/// Constructor
/// </summary>
public DelegateCommand(Action<T> executeMethod, Func<T, bool> canExecuteMethod, bool isAutomaticRequeryDisabled)
{
if (executeMethod == null)
{
throw new ArgumentNullException("executeMethod");
}
_executeMethod = executeMethod;
_canExecuteMethod = canExecuteMethod;
_isAutomaticRequeryDisabled = isAutomaticRequeryDisabled;
}
#endregion
#region Public Methods
/// <summary>
/// Method to determine if the command can be executed
/// </summary>
public bool CanExecute(T parameter)
{
if (_canExecuteMethod != null)
{
return _canExecuteMethod(parameter);
}
return true;
}
/// <summary>
/// Execution of the command
/// </summary>
public void Execute(T parameter)
{
if (_executeMethod != null)
{
_executeMethod(parameter);
}
}
/// <summary>
/// Raises the CanExecuteChaged event
/// </summary>
public void RaiseCanExecuteChanged()
{
OnCanExecuteChanged();
}
/// <summary>
/// Protected virtual method to raise CanExecuteChanged event
/// </summary>
protected virtual void OnCanExecuteChanged()
{
CommandManagerHelper.CallWeakReferenceHandlers(_canExecuteChangedHandlers);
}
/// <summary>
/// Property to enable or disable CommandManager's automatic requery on this command
/// </summary>
public bool IsAutomaticRequeryDisabled
{
get
{
return _isAutomaticRequeryDisabled;
}
set
{
if (_isAutomaticRequeryDisabled != value)
{
if (value)
{
CommandManagerHelper.RemoveHandlersFromRequerySuggested(_canExecuteChangedHandlers);
}
else
{
CommandManagerHelper.AddHandlersToRequerySuggested(_canExecuteChangedHandlers);
}
_isAutomaticRequeryDisabled = value;
}
}
}
#endregion
#region ICommand Members
/// <summary>
/// ICommand.CanExecuteChanged implementation
/// </summary>
public event EventHandler CanExecuteChanged
{
add
{
if (!_isAutomaticRequeryDisabled)
{
CommandManager.RequerySuggested += value;
}
CommandManagerHelper.AddWeakReferenceHandler(ref _canExecuteChangedHandlers, value, 2);
}
remove
{
if (!_isAutomaticRequeryDisabled)
{
CommandManager.RequerySuggested -= value;
}
CommandManagerHelper.RemoveWeakReferenceHandler(_canExecuteChangedHandlers, value);
}
}
bool ICommand.CanExecute(object parameter)
{
// if T is of value type and the parameter is not
// set yet, then return false if CanExecute delegate
// exists, else return true
if (parameter == null &&
typeof(T).IsValueType)
{
return (_canExecuteMethod == null);
}
return CanExecute((T)parameter);
}
void ICommand.Execute(object parameter)
{
Execute((T)parameter);
}
#endregion
#region Data
private readonly Action<T> _executeMethod = null;
private readonly Func<T, bool> _canExecuteMethod = null;
private bool _isAutomaticRequeryDisabled = false;
private List<WeakReference> _canExecuteChangedHandlers;
#endregion
}
/// <summary>
/// This class contains methods for the CommandManager that help avoid memory leaks by
/// using weak references.
/// </summary>
internal class CommandManagerHelper
{
internal static void CallWeakReferenceHandlers(List<WeakReference> handlers)
{
if (handlers != null)
{
// Take a snapshot of the handlers before we call out to them since the handlers
// could cause the array to me modified while we are reading it.
EventHandler[] callees = new EventHandler[handlers.Count];
int count = 0;
for (int i = handlers.Count - 1; i >= 0; i--)
{
WeakReference reference = handlers[i];
EventHandler handler = reference.Target as EventHandler;
if (handler == null)
{
// Clean up old handlers that have been collected
handlers.RemoveAt(i);
}
else
{
callees[count] = handler;
count++;
}
}
// Call the handlers that we snapshotted
for (int i = 0; i < count; i++)
{
EventHandler handler = callees[i];
handler(null, EventArgs.Empty);
}
}
}
internal static void AddHandlersToRequerySuggested(List<WeakReference> handlers)
{
if (handlers != null)
{
foreach (WeakReference handlerRef in handlers)
{
EventHandler handler = handlerRef.Target as EventHandler;
if (handler != null)
{
CommandManager.RequerySuggested += handler;
}
}
}
}
internal static void RemoveHandlersFromRequerySuggested(List<WeakReference> handlers)
{
if (handlers != null)
{
foreach (WeakReference handlerRef in handlers)
{
EventHandler handler = handlerRef.Target as EventHandler;
if (handler != null)
{
CommandManager.RequerySuggested -= handler;
}
}
}
}
internal static void AddWeakReferenceHandler(ref List<WeakReference> handlers, EventHandler handler)
{
AddWeakReferenceHandler(ref handlers, handler, -1);
}
internal static void AddWeakReferenceHandler(ref List<WeakReference> handlers, EventHandler handler, int defaultListSize)
{
if (handlers == null)
{
handlers = (defaultListSize > 0 ? new List<WeakReference>(defaultListSize) : new List<WeakReference>());
}
handlers.Add(new WeakReference(handler));
}
internal static void RemoveWeakReferenceHandler(List<WeakReference> handlers, EventHandler handler)
{
if (handlers != null)
{
for (int i = handlers.Count - 1; i >= 0; i--)
{
WeakReference reference = handlers[i];
EventHandler existingHandler = reference.Target as EventHandler;
if ((existingHandler == null) || (existingHandler == handler))
{
// Clean up old handlers that have been collected
// in addition to the handler that is to be removed.
handlers.RemoveAt(i);
}
}
}
}
}
}
This is an old buggy behaviour, at least in my WPF experience.
I have found that the MvvmLight implementation of RelayCommand (GalaSoft.MvvmLight.CommandWpf.RelayCommand) seems to solve this issue.
Notice that they even create a WPF specific implementation (!) that apparently is targeted to correct this weirdness.

Entity Framework 4 SaveChanges not working and not throwing any error?

I am trying to use my generic repository with a "unit of work" pattern.
Here is my work details
public class GenericRepository:IRepository
{
private readonly string _connectionStringName;
private ObjectContext _objectContext;
private readonly PluralizationService _pluralizer = PluralizationService.CreateService(CultureInfo.GetCultureInfo("en"));
public GenericRepository()
{
this._objectContext = ContextManager.CurrentFor();
}
public void Add<TEntity>(TEntity entity) where TEntity : class
{
((DataEntities.MyTestDBEntities)_objectContext).Countries.AddObject(new Country() { CountryName="UGANDA"});
this._objectContext.AddObject(GetEntityName<TEntity>(), entity);
}
public void Update<TEntity>(TEntity entity) where TEntity : class
{
var fqen = GetEntityName<TEntity>();
object originalItem;
EntityKey key = ObjectContext.CreateEntityKey(fqen, entity);
if (ObjectContext.TryGetObjectByKey(key, out originalItem))
{
ObjectContext.ApplyCurrentValues(key.EntitySetName, entity);
}
}
private string GetEntityName<TEntity>() where TEntity : class
{
return string.Format("{0}.{1}", ObjectContext.DefaultContainerName, _pluralizer.Pluralize(typeof(TEntity).Name));
}
public object Get<TEntity>() where TEntity : class
{
var entityName = GetEntityName<TEntity>();
return ObjectContext.CreateQuery<TEntity>(entityName);
}
public IEnumerable<TEntity> Find<TEntity>(Expression<Func<TEntity, bool>> criteria) where TEntity : class
{
return GetQuery<TEntity>().Where(criteria);
}
private IUnitOfWork unitOfWork;
public ObjectContext ObjectContext
{
get { return ContextManager.CurrentFor(); }
}
public IUnitOfWork UnitOfWork
{
get
{
if (unitOfWork == null)
{
unitOfWork = new UnitOfWork(this.ObjectContext);
}
return unitOfWork;
}
}
public IQueryable<TEntity> GetQuery<TEntity>() where TEntity : class
{
var entityName = GetEntityName<TEntity>();
return ObjectContext.CreateQuery<TEntity>(entityName);
}
}
then I will redirect save changes and other committing the transaction with UnitOfWork.cs
public class UnitOfWork:IUnitOfWork
{
private DbTransaction _transaction;
private ObjectContext _objectContext;
public UnitOfWork(ObjectContext context)
{
_objectContext = context;
}
public bool IsInTransaction
{
get { return _transaction != null; }
}
public void BeginTransaction()
{
BeginTransaction(IsolationLevel.ReadCommitted);
}
public void BeginTransaction(IsolationLevel isolationLevel)
{
if (_transaction != null)
{
throw new ApplicationException("Cannot begin a new transaction while an existing transaction is still running. " +
"Please commit or rollback the existing transaction before starting a new one.");
}
OpenConnection();
_transaction = _objectContext.Connection.BeginTransaction(isolationLevel);
}
public void RollBackTransaction()
{
if (_transaction == null)
{
throw new ApplicationException("Cannot roll back a transaction while there is no transaction running.");
}
try
{
_transaction.Rollback();
}
catch
{
throw;
}
finally
{
ReleaseCurrentTransaction();
}
}
public void CommitTransaction()
{
if (_transaction == null)
{
throw new ApplicationException("Cannot roll back a transaction while there is no transaction running.");
}
try
{
_objectContext.SaveChanges();
_transaction.Commit();
}
catch
{
_transaction.Rollback();
throw;
}
finally
{
ReleaseCurrentTransaction();
}
}
public void SaveChanges()
{
if (IsInTransaction)
{
throw new ApplicationException("A transaction is running. Call BeginTransaction instead.");
}
_objectContext.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);
}
public void SaveChanges(SaveOptions saveOptions)
{
if (IsInTransaction)
{
throw new ApplicationException("A transaction is running. Call BeginTransaction instead.");
}
_objectContext.SaveChanges(saveOptions);
}
/// <summary>
/// Releases the current transaction
/// </summary>
private void ReleaseCurrentTransaction()
{
if (_transaction != null)
{
_transaction.Dispose();
_transaction = null;
}
}
private void OpenConnection()
{
if (_objectContext.Connection.State != ConnectionState.Open)
{
_objectContext.Connection.Open();
}
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Disposes the managed and unmanaged resources.
/// </summary>
/// <param name="disposing"></param>
private void Dispose(bool disposing)
{
if (!disposing)
return;
if (_disposed)
return;
ReleaseCurrentTransaction();
_disposed = true;
}
private bool _disposed;
}
and I am getting my context through my ContextManager class:
public class ContextManager
{
/// <summary>
/// The default connection string name used if only one database is being communicated with.
/// </summary>
public static readonly string DefaultConnectionStringName = "DefaultDb";
/// <summary>
/// An application-specific implementation of IObjectContextStorage must be setup either thru
/// <see cref="InitStorage" /> or one of the <see cref="Init" /> overloads.
/// </summary>
private static IObjectContextStorage Storage { get; set; }
/// <summary>
/// Maintains a dictionary of object context builders, one per database. The key is a
/// connection string name used to look up the associated database, and used to decorate respective
/// repositories. If only one database is being used, this dictionary contains a single
/// factory with a key of <see cref="DefaultConnectionStringName" />.
/// </summary>
// private static Dictionary<string, IObjectContextBuilder<ObjectContext>> objectContextBuilders = new Dictionary<string, IObjectContextBuilder<ObjectContext>>();
private static object _syncLock = new object();
/// <summary>
/// Used to get the current object context session if you're communicating with a single database.
/// When communicating with multiple databases, invoke <see cref="CurrentFor()" /> instead.
/// </summary>
public static ObjectContext Current
{
get { return CurrentFor(); }
}
/// <summary>
/// Used to get the current ObjectContext associated with a key; i.e., the key
/// associated with an object context for a specific database.
///
/// If you're only communicating with one database, you should call <see cref="Current" /> instead,
/// although you're certainly welcome to call this if you have the key available.
/// </summary>
public static ObjectContext CurrentFor()
{
ObjectContext context = null;
lock (_syncLock)
{
if (context == null)
{
context =new TestDAL.DataEntities.MyTestDBEntities();
//Storage.SetObjectContextForKey(key, context);
}
}
return context;
}
/// <summary>
/// This method is used by application-specific object context storage implementations
/// and unit tests. Its job is to walk thru existing cached object context(s) and Close() each one.
/// </summary>
public static void CloseAllObjectContexts()
{
if (CurrentFor().Connection.State == System.Data.ConnectionState.Open)
{
CurrentFor().Connection.Close();
}
}
}
it gives me retrieval of entities, but when I want to create an entity it doesn't shows nay error nor any update in the database.
Any clue will be helpful.
Your public static ObjectContext CurrentFor() method will always create a new context. And your queries are using the ObjectContext property
public ObjectContext ObjectContext
{
get { return ContextManager.CurrentFor(); }
}
Hence you are using multiple instances of ObjectContext. You are calling SaveChanges() of a different instance of ObjectContext. So no changes will be persisted.
Do not handle the transactions explicitly as you did in UnitOfWork. The ObjectContext will do that part.
Your design is a complicated abstraction. Try to use the framework as it is or find a simple Repository pattern which has already being tested an used.

PropertyChangedEventHandler is null in FirePropertyChanged

I have a viewmodel named EmployeeViewModel which is inherited from ViewModelBase. here is the implementation of ViewModelBase.
public event PropertyChangedEventHandler PropertyChanged;
public void FirePropertyChanged(string propertyname)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyname));
}
public void FirePropertyChanged<TValue>(Expression<Func<TValue>> propertySelector)
{
if (PropertyChanged != null)
{
var memberExpression = propertySelector.Body as MemberExpression;
if (memberExpression != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(memberExpression.Member.Name));
}
}
}
My EmployeeViewModel has a property name GridResults, which is bound to the Grid on View, here is the property definition.
public PagedCollectionView GridResults
{
get { return _gridResults; }
set
{
_gridResults = value;
FirePropertyChanged(()=>GridResults);
}
}
Now when i set value of GridResults somewhere in the code in EmployeeViewModel, it fires the property change event and goes into
FirePropertyChanged(Expression> propertySelector)
but inside that method its PropertyChangedEventHandler always remain null and it prevents the complete execution of method. Ultimately my Grid on View remain unnoticed that its underlying itemsource has been changed.
Am i missing something??
Thankx in advance
-K9
Did your ViewModelBase really implement INotifyPropertyChanged?
if yes, please try the FirePropertyChanged with string parameter.
public PagedCollectionView GridResults
{
get { return _gridResults; }
set
{
_gridResults = value;
FirePropertyChanged("GridResults");
}
}
btw here is the INPCBase class i use:
/// <summary>
/// Basisklasse für INotifyPropertyChanged.
/// </summary>
public class INPCBase : INotifyPropertyChanged
{
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Notify mittels PropertyInfo. HINWEIS: diese Variante ist ungefähr 3x langsamer wie
/// <see cref="NotifyPropertyChanged(string)"/> bzw. <see cref="NotifyPropertyChanged(System.ComponentModel.PropertyChangedEventArgs)"/>.
/// </summary>
/// <example>
/// <code>
/// public string InfoMessage
/// {
/// get {return this.infomessage;}
/// set
/// {
/// this.infomessage = value;
/// this.NotifyPropertyChanged(()=> this.InfoMessage);
/// }
/// }
/// </code>
/// </example>
/// <typeparam name="T"></typeparam>
/// <param name="property"></param>
protected void NotifyPropertyChanged<T>(Expression<Func<T>> property)
{
var propertyInfo = ((MemberExpression)property.Body).Member as PropertyInfo;
if (propertyInfo == null)
{
throw new ArgumentException("The lambda expression 'property' should point to a valid Property");
}
this.VerifyPropertyName(propertyInfo.Name);
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyInfo.Name));
}
/// <summary>
/// Notify using pre-made PropertyChangedEventArgs
/// </summary>
/// <param name="args"></param>
protected void NotifyPropertyChanged(PropertyChangedEventArgs args)
{
this.VerifyPropertyName(args.PropertyName);
var handler = PropertyChanged;
if (handler != null)
{
handler(this, args);
}
}
/// <summary>
/// Notify using String property name
/// </summary>
protected void NotifyPropertyChanged(String propertyName)
{
this.VerifyPropertyName(propertyName);
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
#region Debugging Aides
/// <summary>
/// Warns the developer if this object does not have
/// a public property with the specified name. This
/// method does not exist in a Release build.
/// </summary>
[Conditional("DEBUG")]
[DebuggerStepThrough]
public void VerifyPropertyName(string propertyName)
{
// Verify that the property name matches a real,
// public, instance property on this object.
if (TypeDescriptor.GetProperties(this)[propertyName] != null)
return;
var msg = "Invalid property name: " + propertyName;
if (this.ThrowOnInvalidPropertyName)
throw new Exception(msg);
Debug.Fail(msg);
}
/// <summary>
/// Returns whether an exception is thrown, or if a Debug.Fail() is used
/// when an invalid property name is passed to the VerifyPropertyName method.
/// The default value is false, but subclasses used by unit tests might
/// override this property's getter to return true.
/// </summary>
protected virtual bool ThrowOnInvalidPropertyName { get; private set; }
#endregion // Debugging Aides
}