Пример #1
0
        private static void OnEnterCommandPreviewKeyDown(object sender, KeyEventArgs e)
        {
            UITextBox textBox    = sender as UITextBox;
            ICommand  newCommand = GetEnterCommand(textBox);

            if (textBox == null || newCommand == null)
            {
                return;
            }

            if (e.Key == Key.Enter)
            {
                BindingExpression binding = textBox.GetBindingExpression(UITextBox.TextProperty);
                if (binding != null)
                {
                    binding.UpdateSource();
                }

                object parameter = GetEnterCommandParameter(textBox);
                if (newCommand.CanExecute(parameter))
                {
                    newCommand.Execute(parameter);
                }
            }
        }
Пример #2
0
        public ImportView()
        {
            InitializeComponent();

            this.contextMenu.Opened += (s, e) =>
            {
                contextMenu.Items.Clear();


                foreach (ShellAction action in Presenter.GetActions())
                {
                    MenuItem item = new MenuItem();
                    item.Header    = action.Caption;
                    item.IsEnabled = action.IsEnabled && action.IsAuthorized;
                    item.Tag       = action.Id;

                    item.Click += (sender, a) =>
                    {
                        System.Windows.Controls.TextBox textBox = FocusManager.GetFocusedElement(Application.Current.MainWindow) as System.Windows.Controls.TextBox;

                        if (textBox != null)
                        {
                            BindingExpression expression = textBox.GetBindingExpression(System.Windows.Controls.TextBox.TextProperty);

                            if (expression != null)
                            {
                                expression.UpdateSource();
                            }
                        }

                        Presenter.ActionCatalog.Execute((string)((MenuItem)sender).Tag, Presenter.WorkItem, this, null);
                    };

                    contextMenu.Items.Add(item);
                }
            };
        }
Пример #3
0
        /// <summary>
        /// Inialize necessary properties and hook necessary events on TextBox, then add it into tree.
        /// </summary>
        private void BuildTextBox()
        {
            if (_textBox == null)
            {
                _textBox = new TextBox();
                _textBox.BorderThickness = new Thickness(1);
                _textBox.BorderBrush = Brushes.Black;
                _textBox.Background = Brushes.White;
                _textBox.Padding = new Thickness(0);
                _textBox.Margin = new Thickness(0);
                _textBox.KeyDown +=
                    (KeyEventHandler)delegate(object sender, KeyEventArgs args)
                    {
                        if (_editBox.IsEditing && (args.Key == Key.Enter || args.Key == Key.Escape))
                        {
                            if (args.Key == Key.Escape)
                            {
                                BindingExpression bexp = _editBox.GetBindingExpression(EditBox.ActualValueProperty);
                                bexp.UpdateTarget();
                            }
                            _editBox.IsEditing = false;
                            _editBox._canBeEdit = false;
                        }
                    };
                _textBox.GotKeyboardFocus +=
                    (KeyboardFocusChangedEventHandler)delegate(object sender, KeyboardFocusChangedEventArgs e)
                    {
                        TextBox tb = (_textBox as TextBox);
                        tb.SelectionStart = tb.Text.Length;
                    };
                _textBox.LostKeyboardFocus +=
                    (KeyboardFocusChangedEventHandler)delegate(object sender, KeyboardFocusChangedEventArgs e)
                    {
                        BindingExpression bexp = _textBox.GetBindingExpression(TextBox.TextProperty);
                        bexp.UpdateSource();
                        _editBox.IsEditing = false;
                    };
            }

            _canvas = new Canvas();
            _canvas.Children.Add(_textBox);
            _visualChildren.Add(_canvas);

            //Bind Text onto AdornedElement.
            Binding binding = new Binding("ActualValue");
            binding.Mode = BindingMode.TwoWay;
            binding.UpdateSourceTrigger = UpdateSourceTrigger.Explicit;
            binding.Source = _editBox;

            _textBox.SetBinding(TextBox.TextProperty, binding);

            //Binding binding = new Binding("Text");

            //Update TextBox's focus status when layout finishs.
            _textBox.LayoutUpdated += new EventHandler(
                (EventHandler)delegate(object sender, EventArgs args)
                {
                    if (_isVisible)
                        _textBox.Focus();
                });
            _textBox.Background = Brushes.Transparent;
            _textBox.TextWrapping = TextWrapping.NoWrap;

            ScrollViewer.SetCanContentScroll(_textBox, false);
            ScrollViewer.SetVerticalScrollBarVisibility(_textBox, ScrollBarVisibility.Disabled);
            ScrollViewer.SetHorizontalScrollBarVisibility(_textBox, ScrollBarVisibility.Disabled);

            _textBox.GotFocus += delegate
            {
                _textBox.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle,
                    new ThreadStart(delegate
                    {
                        int pos = _textBox.Text.LastIndexOf(".");
                        if (pos == -1)
                            _textBox.SelectAll();
                        else _textBox.Select(0, pos);
                    }));
            };
        }
Пример #4
0
 private static void TextBoxUpdateBinding(TextBox textBox)
 {
     // Source: http://stackoverflow.com/a/5631292/143684
     var be = textBox.GetBindingExpression(TextBox.TextProperty);
     if (be != null)
     {
         be.UpdateSource();
     }
 }
Пример #5
0
 void SetText(TextBox textBox, string value)
 {
     textBox.Text = value;
     textBox.GetBindingExpression(TextBox.TextProperty).UpdateSource();
 }
Пример #6
0
        private void SetError(TextBox textBox, string error)
        {
            var textBoxBindingExpression = textBox.GetBindingExpression(TextBox.TextProperty);
            if (textBoxBindingExpression == null) return;
            ValidationError validationError =
                new ValidationError(new RequiredValidationRule(),
                    textBoxBindingExpression) {ErrorContent = error};


            Validation.MarkInvalid(
                textBoxBindingExpression,
                validationError);
        }
Пример #7
0
        /// <summary>
        ///     Returns a RegexValidationRule to be used for validating the specified TextBox.
        ///     If the TextBox is not yet initialized, this method returns null.
        /// </summary>
        private static RegexValidationRule GetRegexValidationRuleForTextBox(TextBox textBox)
        {
            if (!textBox.IsInitialized)
            {
                // If the TextBox.Text property is bound, but the TextBox is not yet
                // initialized, the property's binding can be null.  In that situation,
                // hook its Initialized event and verify the validation rule again when 
                // that event fires.  At that point in time, the Text property's binding
                // will be non-null.
                EventHandler callback = null;
                callback = delegate
                {
                    textBox.Initialized -= callback;
                    VerifyRegexValidationRule(textBox);
                };
                textBox.Initialized += callback;
                return null;
            }

            // Get the binding expression associated with the TextBox's Text property.
            BindingExpression expression = textBox.GetBindingExpression(TextBox.TextProperty);
            if (expression == null)
                throw new InvalidOperationException(
                    "The TextBox's Text property must be bound for the RegexValidator to validate it.");

            // Get the binding which owns the binding expression.
            Binding binding = expression.ParentBinding;
            if (binding == null)
                throw new ApplicationException(
                    "Unexpected situation: the TextBox.Text binding expression has no parent binding.");

            // Look for an existing instance of the RegexValidationRule class in the
            // binding.  If there is more than one instance in the ValidationRules
            // then throw an exception because we don't know which one to modify.
            RegexValidationRule regexRule = null;
            foreach (ValidationRule rule in binding.ValidationRules)
            {
                if (rule is RegexValidationRule)
                {
                    if (regexRule == null)
                        regexRule = rule as RegexValidationRule;
                    else
                        throw new InvalidOperationException(
                            "There should not be more than one RegexValidationRule in a Binding's ValidationRules.");
                }
            }

            // If the TextBox.Text property's binding does not yet have an 
            // instance of RegexValidationRule in its ValidationRules,
            // add an instance now and return it.
            if (regexRule == null)
            {
                regexRule = new RegexValidationRule();
                binding.ValidationRules.Add(regexRule);
            }

            return regexRule;
        }
Пример #8
0
        private static void PrepareTextBox(TextBox textbox)
        {
            if (textbox == null) return;
            var bindingExpression = textbox.GetBindingExpression(TextBox.TextProperty);
            if (bindingExpression == null) return;
            var binding = bindingExpression.ParentBinding;
            var newBinding = binding.Clone();
            newBinding.ValidatesOnDataErrors = true;
            textbox.SetBinding(TextBox.TextProperty, newBinding);

            if (newBinding.UpdateSourceTrigger == UpdateSourceTrigger.PropertyChanged)
            {
                textbox.TextChanged += OnCoerceText;
            }
            else if (newBinding.UpdateSourceTrigger == UpdateSourceTrigger.LostFocus || newBinding.UpdateSourceTrigger == UpdateSourceTrigger.Default)
            {
                textbox.LostFocus += OnCoerceText;
            }
        }
Пример #9
0
 public MainWindow()
 {
     InitializeComponent();
     TextBox tb = new TextBox();
     tb.GetBindingExpression(null);
 }
Пример #10
0
        /// <summary>
        /// Updates the text in the text box, and updates the source of
        /// the binding so that changes are reflected in the view model.
        /// </summary>
        private void UpdateText(TextBox textBox, string text)
        {
            BindingExpression bindingExpression;

            textBox.Text = text;
            bindingExpression = textBox.GetBindingExpression(TextBox.TextProperty);

            if ((object)bindingExpression != null)
                bindingExpression.UpdateSource();
        }
 private bool Validate(TextBox tb)
 {
     BindingExpression expression = tb.GetBindingExpression(TextBox.TextProperty);
     expression.UpdateSource();
     return !expression.HasError;
 }