Beispiel #1
0
        public void Attach(DataGrid grid)
        {
            var keydown   = Observable.FromEventPattern <KeyEventArgs>(grid, "KeyDown");
            var textInput = Observable.FromEventPattern <TextCompositionEventArgs>(grid, "TextInput");

            var lastEdit = DateTime.MinValue;
            var edit     = textInput
                           .Where(e => NullableConvert.ToUInt32(e.EventArgs.Text) != null && grid.SelectedItem != null)
                           .Do(e => e.EventArgs.Handled = true)
                           .Select(e => new Func <string, string>(v => {
                var text = e.EventArgs.Text;
                var now  = DateTime.Now;
                if ((now - lastEdit) > inputInterval)
                {
                    return(text);
                }
                else
                {
                    return(v + text);
                }
            }));

            var backspace = keydown.Where(e => e.EventArgs.Key == Key.Back && grid.SelectedItem != null)
                            .Do(e => e.EventArgs.Handled = true)
                            .Select(e => new Func <string, string>(v => v.Slice(0, -2)));

            var delete = keydown.Where(e => e.EventArgs.Key == Key.Delete && grid.SelectedItem != null)
                         .Do(e => e.EventArgs.Handled = true)
                         .Select(e => new Func <string, string>(v => ""));

            var updated = edit.Merge(backspace).Merge(delete);

            updated.Subscribe(a => {
                UpdateValue(grid, a);
                lastEdit = DateTime.Now;
            });

            //игнорировать события до тех пор пока не произошло событие редактирования
            //когда произошло взять одно событие и повторить, фактически это state machine
            //которая генерирует событие OfferCommitted только если было событие редактирования
            updated.Throttle(commitInterval, scheduler)
            .Merge(Observable.FromEventPattern <EventArgs>(grid, "CurrentCellChanged").Select(e => e.Sender))
            .Merge(Observable.FromEventPattern <RoutedEventArgs>(grid, "Unloaded").Select(e => e.Sender))
            .SkipUntil(updated)
            .Take(1)
            .Repeat()
            .ObserveOn(scheduler)
            .Subscribe(e => {
                lastEdit   = DateTime.MinValue;
                var editor = GetEditor(grid);
                if (editor != null)
                {
                    editor.Committed();
                }
                else
                {
                    ViewModelHelper.InvokeDataContext(grid, "OfferCommitted");
                }
            });
        }
        public void Invoke_with_anonymous_object()
        {
            var param = new { Method = "Test" };
            var args  = (ExecutedRoutedEventArgs)typeof(ExecutedRoutedEventArgs)
                        .GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { typeof(ICommand), typeof(object) }, null)
                        .Invoke(new object[] { new RoutedUICommand(), param });
            var result = ViewModelHelper.InvokeDataContext(element, args);

            Assert.AreEqual(1, model.Count);
        }
Beispiel #3
0
        private void UpdateValue(object sender, Func <string, string> value)
        {
            var dataGrid = (DataGrid)sender;
            var item     = dataGrid.SelectedItem as IInlineEditable;

            if (item == null)
            {
                return;
            }
            item.Value = SafeConvert.ToUInt32(value(item.Value == 0 ? "" : item.Value.ToString()));
            var editor = GetEditor(dataGrid);

            if (editor != null)
            {
                editor.Updated();
            }
            else
            {
                ViewModelHelper.InvokeDataContext(dataGrid, "OfferUpdated");
            }
        }
 private void Open(object sender, RoutedEventArgs e)
 {
     ViewModelHelper.InvokeDataContext(this, "Open", ((FrameworkContentElement)sender).DataContext);
 }
 public void Invoke_view_model()
 {
     ViewModelHelper.InvokeDataContext(element, "Test", EventArgs.Empty);
     Assert.AreEqual(1, model.Count);
 }
        public void Return_result()
        {
            var result = ViewModelHelper.InvokeDataContext(element, "CanTest");

            Assert.That(result, Is.EqualTo(false));
        }
 public void Invoke_result()
 {
     ViewModelHelper.InvokeDataContext(element, "Test");
     Assert.That(model.Count, Is.EqualTo(1));
 }