Helpers for using System.Reactive with INotifyPropertyChanged
.
When used from .Net 4.6 assembly redirects are needed. dotnet/reactive#299
<dependentAssembly>
<assemblyIdentity name="System.Reactive.Core" publicKeyToken="94bc3704cddfc263" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.0.3000.0" newVersion="3.0.3000.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Reactive.PlatformServices" publicKeyToken="94bc3704cddfc263" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.0.3000.0" newVersion="3.0.3000.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Reactive.Linq" publicKeyToken="94bc3704cddfc263" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.0.3000.0" newVersion="3.0.3000.0" />
</dependentAssembly>
Nuget can generate redirects using PM> Get-Project –All | Add-BindingRedirect
var subscription = foo.ObservePropertyChangedSlim(x => x.Bar.Baz)
.Subscribe(...);
- Return an
IObservable<PropertyChangedEventArgs>
so more lightweight thanObservePropertyChanged
- Filters change args mathing property name or
string.IsNullOrEmpty
Default true meaning that the observable will call OnNext on Subscribe
Observe the value of a property, calls on next when the value changes.
Returns an Observable<Maybe<T>>
, this is due to if you observe foo.ObserveValue(x => x.Bar.Baz)
and Bar is null.
If so it return Maybe.None
var ints = new List<int>();
foo.ObserveValue(x => x.Bar.Baz)
.Subscribe(ints.Add);
var subscription = fake.ObservePropertyChanged(x => x.Level1.Level2.Value)
.Subscribe(...);
- Create an observable from the
PropertytChangedEvent
for fake. - Listens to nested changes. All steps in the property path must be INotifyPropertyChanged. Throws if not.
- When PropertyChanged is raised with string.Empty or null the observable notifies.
- Updates subscriptions for items in path and uses weak events.
Default true meaning that the observable will call OnNext on Subscribe.
The sender will be tha last node in the path that has a value, in the example above it would be the value of the property Level2
if it is not null, then Level1
if not null if the entire path is null the root item fake
is used as sender for the first notifixcation.
The eventags for the signal initial event is string.Empty
fake.ObservePropertyChangedWithValue(x => x.Collection)
.ItemPropertyChanged(x => x.Name)
.Subscribe(_changes.Add);
var subscription = collection.ObserveCollectionChanged()
.Subscribe(...);
- Create an observable from the
CollectionChangedEvent
for collection.
Default true meaning that the observable will call OnNext on Subscribe
var subscription = collection.ObserveCollectionChangedSlim()
.Subscribe(...);
- Return an
IObservable<NotifyCollectionChangedEventArgs>
so more lightweight thanObserveCollectionChanged
Default true meaning that the observable will call OnNExt on Subscribe
var subscription = collection.ObserveItemPropertyChanged(x => x.Name)
.Subscribe(...);
An observable that signals when the collection is modified or the property that the lambda points to signals for any item in the collection. If the collection changes the collection is sender. When an element is removed null is passed as item. When an element is replaced the observable notifies twice, once for the remove of the old element and once for the add of the new element.
Default true meaning that the observable will call OnNExt on Subscribe
var subscription = collection.ObserveItemPropertyChangedSlim(x => x.Name)
.Subscribe(...);
An observable that signals when the collection is modified or the property that the lambda points to signals for any item in the collection.
Default true meaning that the observable will call OnNExt on Subscribe
A type that calculates IsSatisfied
when any of the IObservable<object>
triggers signals.
Create it in code like this:
this.isTrueCondition = new Condition(
this.ObservePropertyChanged(x => x.IsTrue),
() => this.IsTrue);
The conditions work really well when used with an IoC. Then subclasses are created and the IoC is used to build trees of nested conditions.
public class HasFuel : Condition
{
public HasFuel(Car car)
: base(
car.ObservePropertyChanged(x => x.FuelLevel),
() => car.FuelLevel > 0)
{
}
}
Evaluates the criteria passed to the ctor. Recalculates when any of the observables signals and the value changes.
Default is GetType.Name
but the property is mutable so other names can be specified.
The last 100 times of change and values for IsSatisfied
Returns a condition wrapping the instance and negating the value of IsSatisfied
.
Negating a negated condition returns the original condition.
Calculates IsSatisfied
based on if any of the prerequisites are true. Listens to changes in IsSatisfied
for prerequisites and notifies when value changes.
public class IsAnyDoorOpen : OrCondition
{
public IsAnyDoorOpen(
IsLeftDoorOpen isLeftDoorOpen,
IsRightDoorOpen isRightDoorOpen)
: base(isLeftDoorOpen, isRightDoorOpen)
{
}
}
True if IsSatisfied
for any prerequisites is true.
False if IsSatisfied
for all prerequisites are false.
Null if IsSatisfied
for no prerequisite is true and any prerequisite is null.
Calculates IsSatisfied
based on if all of the prerequisites have IsSatisfied == true. Listens to changes in IsSatisfied
for prerequisites and notifies when value changes.
public class IsAnyDoorOpen : AndCondition
{
public IsAnyDoorOpen(
IsLeftDoorOpen isLeftDoorOpen,
IsRightDoorOpen isRightDoorOpen)
: base(isLeftDoorOpen.Negate(), isRightDoorOpen.Negate())
{
}
}
True if IsSatisfied
for all prerequisites are true.
False if IsSatisfied
for any prerequisite is false.
Null if IsSatisfied
for no prerequisite is false and any prerequisite is null.
Se demo for more code samples.
A typed filtered view of a collection. Sample usage:
public sealed class ViewModel : INotifyPropertyChanged, IDisposable
{
private string filterText;
private bool disposed;
public ViewModel(ObservableCollection<Person> people, IWpfSchedulers schedulers)
{
this.FilteredPeople = people.AsReadOnlyFilteredView(
this.IsMatch,
TimeSpan.FromMilliseconds(10),
schedulers.Dispatcher,
this.ObservePropertyChangedSlim(nameof(this.FilterText)));
}
public event PropertyChangedEventHandler PropertyChanged;
public IReadOnlyObservableCollection<Person> FilteredPeople { get; }
public string FilterText
{
get
{
return this.filterText;
}
set
{
if (value == this.filterText)
{
return;
}
this.filterText = value;
this.OnPropertyChanged();
}
}
public void Dispose()
{
if (this.disposed)
{
return;
}
this.disposed = true;
this.FilteredPeople.Dispose();
}
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private bool IsMatch(Person person)
{
if (string.IsNullOrEmpty(this.filterText))
{
return true;
}
var indexOf = CultureInfo.InvariantCulture.CompareInfo.IndexOf(person.FirstName, this.filterText, CompareOptions.OrdinalIgnoreCase);
if (this.filterText.Length == 1)
{
return indexOf == 0;
}
return indexOf >= 0;
}
}
A view that maps from one type to another. If the source type is a reference type the same instance produces the same mapped instance if the item appears more than once in the collection.
public sealed class ViewModel : IDisposable
{
private bool disposed;
public ViewModel(ObservableCollection<Person> people)
{
this.PersonViewModels = people.AsMappingView(x => new PersonViewModel(x));
}
public MappingView<Person, PersonViewModel> PersonViewModels { get; }
public void Dispose()
{
if (this.disposed)
{
return;
}
this.disposed = true;
this.PersonViewModels.Dispose();
}
}
Pass in an action that is invoked when the last instance is removed from the mapped collection.
public sealed class ViewModel : IDisposable
{
private bool disposed;
public ViewModel(ObservableCollection<Person> people)
{
this.PersonViewModels = people.AsMappingView(
x => new PersonViewModel(x),
x => x.Dispose());
}
public IReadOnlyObservableCollection<PersonViewModel> PersonViewModels { get; }
public void Dispose()
{
if (this.disposed)
{
return;
}
this.disposed = true;
(this.PersonViewModels as IDisposable)?.Dispose();
}
}
A view that buffers changes. If there are many changes within the buffer time one reset event is raised instead of on event per change.
public sealed class ViewModel : IDisposable
{
private bool disposed;
public ViewModel(ObservableCollection<Person> people)
{
this.PersonViewModels = people.AsReadOnlyThrottledView(TimeSpan.FromMilliseconds(100));
}
public IReadOnlyObservableCollection<Person> PersonViewModels { get; }
public void Dispose()
{
if (this.disposed)
{
return;
}
this.disposed = true;
this.PersonViewModels.Dispose();
}
}
A view that notifies on the dispatcher. Useful if the collection is updated on another thread.
public sealed class ViewModel : IDisposable
{
private bool disposed;
public ViewModel(ObservableCollection<Person> people)
{
this.PersonViewModels = people.AsReadOnlyDispatchingView();
}
public IReadOnlyObservableCollection<Person> PersonViewModels { get; }
public void Dispose()
{
if (this.disposed)
{
return;
}
this.disposed = true;
this.PersonViewModels.Dispose();
}
}
A view that can have source set to different collections. Useful when composing with for example MappedView
public sealed class ViewModel : IDisposable
{
private bool disposed;
public ViewModel(ObservableCollection<Person> people)
{
this.Update(people);
}
public ReadOnlySerialView<Person> People { get; } = new ReadOnlySerialView<Person>();
public void Update(ObservableCollection<Person> people)
{
this.People.SetSource(people);
}
public void Dispose()
{
if (this.disposed)
{
return;
}
this.disposed = true;
this.People.Dispose();
}
}
The following sample filters a list and then maps it to another type. This happens on the task pool. Then changhes are notified to the view on the dispatcher.
public sealed class ViewModel : INotifyPropertyChanged, IDisposable
{
private string filterText;
private bool disposed;
public ViewModel(ObservableCollection<Person> people, IWpfSchedulers schedulers)
{
this.FilteredPeople = people.AsReadOnlyFilteredView(
this.IsMatch,
TimeSpan.FromMilliseconds(10),
schedulers.TaskPool,
this.ObservePropertyChangedSlim(nameof(this.FilterText)))
.AsMappingView(
x => new PersonViewModel(x),
x => x.Dispose())
.AsReadOnlyDispatchingView();
}
public event PropertyChangedEventHandler PropertyChanged;
public IReadOnlyObservableCollection<PersonViewModel> FilteredPeople { get; }
public string FilterText
{
get
{
return this.filterText;
}
set
{
if (value == this.filterText)
{
return;
}
this.filterText = value;
this.OnPropertyChanged();
}
}
public void Dispose()
{
if (this.disposed)
{
return;
}
this.disposed = true;
(this.FilteredPeople as IDisposable)?.Dispose();
}
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private bool IsMatch(Person person)
{
if (string.IsNullOrEmpty(this.filterText))
{
return true;
}
var indexOf = CultureInfo.InvariantCulture.CompareInfo.IndexOf(person.FirstName, this.filterText, CompareOptions.OrdinalIgnoreCase);
if (this.filterText.Length == 1)
{
return indexOf == 0;
}
return indexOf >= 0;
}
}
Helpers for using System.Reactive with INotifyPropertyChanged
in WPF applications.
A set of relay commands. The generic versions take a command parameter of the generic type. The non-generic version does not use the command parameter.
For executing tasks. If the overload that takes a CancellationToken
is used the CancelCommand
cancels the execution.
By default the command is disabled while running.
If no condition is passed in IsEnabled is true when not running.
public ViewModel()
{
var canExecute = new Condition(
this.ObservePropertyChanged(x => x.CanExecute),
() => this.CanExecute);
this.SimpleTaskCommand = new AsyncCommand(this.SimpleTask, canExecute);
this.CancelableTaskCommand = new AsyncCommand(this.CancelableTask, canExecute);
this.ParameterTaskCommand = new AsyncCommand<string>(this.ParameterTask, canExecute);
this.CancelableParameterTaskCommand = new AsyncCommand<string>(this.CancelableParameterTask, canExecute);
}
public AsyncCommand SimpleTaskCommand { get; }
public AsyncCommand CancelableTaskCommand { get; }
public AsyncCommand ParameterTaskCommand { get; }
public AsyncCommand CancelableParameterTaskCommand { get; }
private async Task SimpleTask()
{
await Task.Delay(500).ConfigureAwait(false);
}
private async Task CancelableTask(CancellationToken token)
{
this.Count = 0;
for (int i = 0; i < 5; i++)
{
token.ThrowIfCancellationRequested();
this.Count++;
await Task.Delay(this.Delay, token).ConfigureAwait(false);
}
}
private Task ParameterTask(string arg)
{
return this.SimpleTask();
}
private Task CancelableParameterTask(string arg, CancellationToken token)
{
return this.CancelableTask(token);
}
A relay command where CanExecute
is controlled by a ICondition
public ViewModel()
{
var canExecute = new Condition(
this.ObservePropertyChanged(x => x.CanExecute),
() => this.CanExecute);
this.ConditionRelayCommand = new ConditionRelayCommand(() => ..., canExecute);
this.ConditionRelayCommandWithParameter = new ConditionRelayCommand<string>(parameter => ..., canExecute);
}
public ConditionRelayCommand ConditionRelayCommand { get; }
public ConditionRelayCommand ConditionRelayCommandWithParameter { get; }
A command where you need to manually call RaiseCanExecuteChanged
.
public ViewModel()
{
this.ManualRelayCommand = new ManualRelayCommand(() => ..., () => this.CanExecute);
this.ManualRelayCommandWithParameter = new ManualRelayCommand<string>(parameter => ..., () => this.CanExecute);
}
public ManualRelayCommand ManualRelayCommand { get; }
public ManualRelayCommand ManualRelayCommandWithParameter { get; }
A command where an observable is passed in for raising CanExecuteChanged
.
public ViewModel()
{
var onCanExecute = this.ObservePropertyChanged(x => x.CanExecute)
this.ObservingRelayCommand = new ObservingRelayCommand(() => ..., onCanExecute, () => this.CanExecute);
this.ObservingRelayCommandWithParameter = new ObservingRelayCommand<string>(parameter => ..., onCanExecute, () => this.CanExecute);
}
public ObservingRelayCommand ObservingRelayCommand { get; }
public ObservingRelayCommand ObservingRelayCommandWithParameter { get; }
A command that uses the CommandManager.RequerySuggested
event.
It exposes a RaiseCanExecuteChanged method for forcing notification.
public ViewModel()
{
this.RelayCommand = new RelayCommand(() => ..., () => this.CanExecute);
this.RelayCommandWithParameter = new RelayCommand<string>(parameter => ..., () => this.CanExecute);
}
public RelayCommand ManualRelayCommand { get; }
public RelayCommand ManualRelayCommandWithParameter { get; }
Markupextension for getting enum values for a type.
Sample code:
xmlns:reactive="http://Gu.com/Reactive"
...
<ComboBox ItemsSource="{reactive:EnumValuesFor {x:Type Visibility}}" />
Markupextension for binding when not in the visual tree.
Sample code:
xmlns:reactive="http://Gu.com/Reactive"
...
<CheckBox x:Name="CheckBox" IsChecked="{Binding Visible}" />
...
<DataGrid AutoGenerateColumns="False">
<DataGrid.Columns>
<!--Here the viewmodel has a Visibility property-->
<DataGridTextColumn Header="Binding"
Visibility="{reactive:NinjaBinding {Binding Visibility}}" />
<DataGridTextColumn Header="ElementName"
Visibility="{reactive:NinjaBinding Binding={Binding IsChecked,
ElementName=CheckBox,
Converter={StaticResource BooleanToVisibilityConverter}}}" />
</DataGrid.Columns>
</DataGrid>