Wpf detect when notifypropertychanged has been fired - wpf

I would like for my ui to perform some functions whenever the bound data has been modified.
Is it possible for the view to execute some code after the notifychange event has been called (due to changes in the underlying model)

If your model implements INotifyPropertyChanged, you can subscibe to PropertyChanged event of it.
model.PropertyChanged += new PropertyChangedEventHandler(Model_PropertyChanged);
void Model_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
}
}
and in the handler you can check which property is changed and do your work accordingly

Related

Notification of Collection Update for IDataErrorInfo Validation

I would like to notify the binding system in WPF of a change in a collection's item so that a validation through IDataErrorInfo gets reevaluated whenever an item inside a collection changes.
I have a custom list type which implements INotifyCollectionChanged (and works properly). But somehow the validation logic is never called, because (or at least I am assuming) that this notification does not reach the right place. Is this scenario even possible? What have I missed?
[Edit]
So basically the "architecture" is the following:
MVVM base class implements IDataErrorInfo and you can register DataValidators with lambdas in the derived MVVM classes, such as:
RegisterDataValidator(() => People, () => (People.Count == 0) ? "At least one person must be specified" : null);
The indexer on the base class checks the registered validator and returns the returned by it.
I have a SmartBindingList<T> where T: INotifyPropertyChange which is basically a list that when items are added to it, registers the items' PropertyChangedEvent and reacts to these events by firing the CollectionChanged event on the class itself:
private void OnSubPropertyChanged (object sender, PropertyChangedEventArgs e)
{
if (sender is T1)
{
if (CollectionChanged != null)
{
NotifyCollectionChangedEventArgs eventArgs = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, sender, sender);
CollectionChanged(this, eventArgs);
}
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(myPropertyName));
}
}
}
So all this works nicely, but when the code runs on the CollectionChanged(this, eventArgs) line, nothing happens in terms of validation. It should be wired up correctly, because when I add something to the collection, it works perfectly. What am I missing?
This is a bit of stab in the dark without some example code, but try raising your OnPropertyChanged notification for the properties that have changed. This should cause validation to be re-evaluated.

How to know when binding is completed?

When I set the .ItemSource() property on a DataGrid to a Collection, the call returns fast, but the actual binding happens afterwards. Since I want to display a waiting cursor, I need to detect when the actual binding has finished. Is there any event for this?
Anything based on ItemsControl uses an ItemContainerGenerator to generate its items in the background. You can access the ItemContainerGenerator property of the DataGrid and hook up the StatusChanged event to determine when it's done. If you're using virtualization and scroll, this will fire again so you need to handle that if necessary in your case.
I waited for my DataGrid's Loaded event to fire, and I did a BeginInvoke, like this:
private void SubjectsList_Loaded(object sender, RoutedEventArgs e)
{
Dispatcher.BeginInvoke(DispatcherPriority.Render, new Action(() => ColorMyRows()));
}
More details available in my answer here: https://stackoverflow.com/a/44464630/2101117
Your best bet is to hook into OnPropertyChanged event in your Window or User Control. This event is fired every time a property is updated. Then check for the actual property you wish to observe and take action.
protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
{
if ("YOUR_PROPERTY_NAME".Equals(e.Property.ToString()))
{
// Take action
}
base.OnPropertyChanged(e);
}

WPF: How do I hook into a ListView's ItemsSource CollectionChanged notification?

I have a ListView that is databound to an ObservableCollection ...
<ListView x:Name="List1" ItemsSource="{Binding MyList}" />
I can't seem to find any event that are triggered when the collection changes, so I'm thinking that somehow I need to hook into the collectionchanged notification somehow? I'm not really sure how to do that.
Basically, when the collection changes I want to do additional work beyond what the ListView already does in updating it's list.
By default the ItemsSource is of type IEnumerable. You need to first cast to a type that has access to the CollectionChanged event, then add a handler for that event.
((INotifyCollectionChanged)List1.ItemsSource).CollectionChanged +=
new NotifyCollectionChangedEventHandler(List1CollectionChanged);
public void List1CollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
{
// Your logic here
}
Note: I cast it to INotifyCollectionChanged in my example, but you can really cast it to any object that implements that. Though, as a best practice, you should cast to the most generic type that gives you access to the methods/properties/events you need. So, while you can cast it to an ObservableCollection, you don't need to. INotifyCollectionChanged contains the event you need and if you ever decide to use some other type of collection that implements it, this will continue to work, whereas casting to an ObservableCollection means that if you one day decide that you're list is now of type MyOwnTypeOfObservableCollectionNotDerivedFromObservableCollection than this will break. ;)
P.S. This should go in the xaml code-behind.
you are going to have to attach the handler to your list. Or, use a CollectionView and hook the changed event there.
in your codebehind, do like this:
MyList.CollectionChanged += new NotifyCollectionChangedEventHandler( this.MyCollectionChanged );
private void SortCollectionChanged( object sender, NotifyCollectionChangedEventArgs e )
{
Debug.WriteLine( "Changed" );
}
An ObservableCollection{T} exposes the INotifyCollectionChanged.CollectionChanged event. When binding to an ItemsSource the data binding engine handles the propogation of changes from the source to the items control, but if you need to perform additional processing you can attach a handler to the CollectionChanged event and use the NotifyCollectionChangedEventArgs it provides.
Assuming you have a public property on your view model named MyList:
public ObservableCollection<T> MyList
{
get
{
if(_viewModelMyList == null)
{
_viewModelMyList = new ObservableCollection<T>;
_viewModelMyList.CollectionChanged += (o, e) =>
{
// code to process change event can go here
if(e.Action == NotifyCollectionChangedAction.Add)
{
}
};
}
return _viewModelMyList;
}
}
private ObservableCollection<T> _viewModelMyList;

Changing collection from CollectionChanged event

I want to update collection after it was changed but I can't seem to get "away" from this exception:
Cannot change ObservableCollection during a CollectionChanged or PropertyChanged event.
Inside event handler I unsubscribe from Collection changed event before changing anything to prevent infinite loops and after changes are made i subscribe again to same event.
private void CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
data.CollectionChanged -= CollectionChanged;
data.Add("Item");
data.CollectionChanged += CollectionChanged;
}
I tried using Dispatcher to call data.Add("Item"), but no luck :(
The problem is that you are unsubscribing from the event within the event which has yet to complete. Drop back and re-evaluate why you are adding to the collection and determine if there is another way to accomplish what you need.

WPF Routed events firing?

I have a WPF form with 3 buttons and have routed events on them, commands are binded on start...
private void InitCommandBinding(UIElement frameworkElement) {
CommandBinding commandBinding;
commandBinding = new CommandBinding(ViewModelCommands.Save, Save_Executed, Save_CanExecute);
frameworkElement.CommandBindings.Add(commandBinding);
commandBinding = new CommandBinding(ViewModelCommands.SaveAndClose, SaveAndClose_Executed, SaveAndClose_CanExecute);
frameworkElement.CommandBindings.Add(commandBinding);
commandBinding = new CommandBinding(ViewModelCommands.Delete, Delete_Executed, Delete_CanExecute);
frameworkElement.CommandBindings.Add(commandBinding);
}
the details ui has code like
private void Delete_Executed(object sender, ExecutedRoutedEventArgs e) {
try
{do validations }
}
private void Delete_CanExecute(object sender, CanExecuteRoutedEventArgs e) {
e.CanExecute = viewModel.IsValid(); (returns bool)
}
Validity enables and disables buttons etc.
The form has an instance of an object new or old and validations take place on the data
My issue is that the event just excute all the time and the form just hangs cause validation code does poll db etc to check....
how to I just get them to fire once when the form is loaded mmm....
If I understand you well it is only necessary to check the validity of the data at form load and the IsValid method is resource intensive?
Why don't you change the IsValid() method to an IsValid property and set this is in the Form_Loaded event?
The CanExute method will be checked any time the UI fires an event like TextChanged, LostFocus etc. So you better make such methods very lightweight.

Resources