Let's say I have two observables Obs1 and Obs2. I want a trigger on Obs1 to suppress the subsequent trigger on Obs2 (marble diagram below). How do I do this?
Obs1---x---x--x----
Obs2----yyy-yy-yyyy
Rslt-----yy--y--yyy
Specifically, I have a class with two properties, Target and SelectedItem. When the target is set, SelectedItem should be set immediately based on a SpecialValue property on the target. Users should be able to change the selection, in which case the new value gets propagated back to the target. SelectedItem should propagate back to the target only if the user changes the value; however, the value is propagating back to the target the moment the target is set - this is the undesired behavior I'm trying to fix.
(SelectionViewModel leverages ReactiveUI, but we mimicked Prism's SetProperty method to aid in migration. BindToProperty is just a helper method that does what it says.)
sealed class SelectionViewModel
{
internal SelectionViewModel( )
{
this.WhenAnyValue(x => x.Target).Where(t => t != null)
.Select(_ => Target.SpecialValue)
.BindToProperty(this, x => x.SelectedItem);
this.WhenAnyValue(x => x.Target).Where(t => t != null)
.Select(_ => this.WhenAnyValue(x => x.SelectedItem).Skip(1))
.Switch()
.BindToProperty(this, x => Target.SpecialValue);
}
private MyClass _selectedItem;
public MyClass SelectedItem
{
get { return _selectedItem; }
set { SetProperty(ref _selectedItem, value); }
}
private ITarget _target;
public ITarget Target
{
get { return _target; }
set { SetProperty(ref _target, value); }
}
}