public void BindCommandToObjectTargetIsNull()
        {
            var vm   = new CommandBindingViewModel();
            var view = new CommandBindingView {
                ViewModel = vm
            };

            var invokeCount = 0;

            vm.Command2.Subscribe(_ => invokeCount++);

            var sub = new Subject <object>();

            Assert.Throws <Exception>(() =>
            {
                var disp = CreatesCommandBinding.BindCommandToObject(vm.Command2, true, sub);

                view.Command2.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left)
                {
                    RoutedEvent = UIElement.MouseUpEvent
                });

                disp.Dispose();

                view.Command2.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left)
                {
                    RoutedEvent = UIElement.MouseUpEvent
                });
            });

            Assert.Equal(0, invokeCount);
        }
        public void CommandBindToExplicitEventWireup()
        {
            var vm   = new CommandBindingViewModel();
            var view = new CommandBindingView {
                ViewModel = vm
            };

            var invokeCount = 0;

            vm.Command2.Subscribe(_ => invokeCount++);

            var disp = view.BindCommand(vm, x => x.Command2, x => x.Command2, "MouseUp");

            view.Command2.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left)
            {
                RoutedEvent = UIElement.MouseUpEvent
            });

            disp.Dispose();

            view.Command2.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left)
            {
                RoutedEvent = UIElement.MouseUpEvent
            });
            Assert.Equal(1, invokeCount);
        }
        public void BindCommandToObjectWithEventCommandIsArgumentNull()
        {
            var vm   = new CommandBindingViewModel();
            var view = new CommandBindingView {
                ViewModel = vm
            };

            var invokeCount = 0;

            vm.Command2.Subscribe(_ => invokeCount++);
            var btn = new Button();
            var cmd = (btn as ICommand) !;
            var sub = new Subject <object>();

            Assert.Throws <TargetInvocationException>(() =>
            {
                var disp = CreatesCommandBinding.BindCommandToObject(cmd, view, sub, "PropertyChanged");

                view.Command2.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left)
                {
                    RoutedEvent = UIElement.MouseUpEvent
                });

                disp.Dispose();

                view.Command2.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left)
                {
                    RoutedEvent = UIElement.MouseUpEvent
                });
            });

            Assert.Equal(0, invokeCount);
        }
            static (IDisposable, WeakReference) GetWeakReference()
            {
                var vm   = new CommandBindingViewModel();
                var view = new CommandBindingView {
                    ViewModel = vm
                };
                var weakRef = new WeakReference(vm);
                var disp    = view.BindCommand(vm, x => x.Command2, x => x.Command2, "MouseUp");

                view.ViewModel = new CommandBindingViewModel();

                return(disp, weakRef);
            }
        public void CommandBindViewModelToViewWithFunc()
        {
            var vm   = new CommandBindingViewModel();
            var view = new CommandBindingView {
                ViewModel = vm
            };

            // Create a paramenter feed
            vm.Command2.Subscribe(_ =>
            {
                vm.Value++;
            });
            view.BindCommand(vm, x => x.Command2, x => x.Command2, "MouseUp");

            // Bind the command and the Func<T> parameter.
            var fixture = new CommandBinderImplementation().BindCommand(vm, view, vm => vm.Command1, v => v.Command3, vm => vm.Value, "MouseUp");

            Assert.Equal(0, vm.Value);

            // Confirm that the values update as expected.
            var parameter = 0;

            vm.Command1.Subscribe(i => parameter = i);
            view.Command2.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left)
            {
                RoutedEvent = UIElement.MouseUpEvent
            });
            Assert.Equal(1, vm.Value);
            Assert.Equal(0, parameter);

            view.Command3.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left)
            {
                RoutedEvent = UIElement.MouseUpEvent
            });
            Assert.Equal(1, parameter);

            view.Command2.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left)
            {
                RoutedEvent = UIElement.MouseUpEvent
            });
            Assert.Equal(2, vm.Value);
            Assert.Equal(1, parameter);

            view.Command3.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left)
            {
                RoutedEvent = UIElement.MouseUpEvent
            });
            Assert.Equal(2, parameter);
            Assert.Equal(2, vm.Value);
        }