/// <summary> /// Adds a command binding to the specified <paramref name="element"/> and returns an observable sequence with two notification channels, /// with the right channel receiving notifications when the <paramref name="command"/> is executed and the left channel /// receiving notifications when the <paramref name="command"/> is queried as to whether it can be executed. /// </summary> /// <param name="command">The <see cref="ICommand"/> from which notifications are received.</param> /// <param name="element">The <see cref="UIElement"/> that queries or executes the specified <paramref name="command"/>.</param> /// <returns>An observable sequence with two notification channels, with the right channel receiving notifications when the <paramref name="command"/> is executed and /// the left channel receiving notifications when the <paramref name="command"/> is queried as to whether it can be executed.</returns> public static IObservable <Either <EventPattern <CanExecuteRoutedEventArgs>, EventPattern <ExecutedRoutedEventArgs> > > AsObservable( this ICommand command, UIElement element) { Contract.Requires(command != null); Contract.Requires(element != null); Contract.Ensures(Contract.Result <IObservable <Either <EventPattern <CanExecuteRoutedEventArgs>, EventPattern <ExecutedRoutedEventArgs> > > >() != null); return(Observable2.CreateEither <EventPattern <CanExecuteRoutedEventArgs>, EventPattern <ExecutedRoutedEventArgs> >( observer => { var binding = new CommandBinding( command, (sender, e) => observer.OnNextRight(new EventPattern <ExecutedRoutedEventArgs>(sender, e)), (sender, e) => observer.OnNextLeft(new EventPattern <CanExecuteRoutedEventArgs>(sender, e))); element.CommandBindings.Add(binding); return () => element.CommandBindings.Remove(binding); })); }