xsharp.eu • MVVM: how to build a control with databinding or enter key in a TextBox
Page 1 of 1

MVVM: how to build a control with databinding or enter key in a TextBox

Posted: Tue Jun 13, 2017 9:48 am
by wriedmann
If you need to build user friendly applications, for sure you need to react also on specific key presses in a TextBox control.
MVVM requires you to use DataBinding.

This is the source for such a control, a sample with the full source is attached as XIDE application export file.

Code: Select all

using System.Windows
using System.Windows.Controls
using System.Windows.Input

class EnterTextBox inherit TextBox 
protect _oEnterCommand as ICommand
public static initonly EnterCommandProperty as DependencyProperty
	
static constructor()

  EnterTextBox.EnterCommandProperty := DependencyProperty.Register( ;
    "EnterCommand", TypeOf( ICommand ), TypeOf( EnterTextBox ), ;
    UIPropertyMetadata{ null, PropertyChangedCallback{ EnterCommandPropertyChanged } } )

  return          
	
static method EnterCommandPropertyChanged( d as DependencyObject, ;
    e as DependencyPropertyChangedEventArgs ) as void
  local oEnterTextBox as EnterTextBox
  local oCommand as ICommand
	
  oEnterTextBox := ( EnterTextBox ) d
  oCommand := ( ICommand ) e:NewValue
  oEnterTextBox:_EnterCommand := oCommand
	
  return

property _EnterCommand as ICommand
  set
    _oEnterCommand := value
    if _oEnterCommand == null
      self:KeyDown -= KeyDownEx
    else
      self:KeyDown += KeyDownEx
    endif
  end set
  get   
    return _oEnterCommand
  end get
end property  

method KeyDownEx( oSender as object, e as KeyEventArgs ) as void
	
  if e:Key:Equals( Key.Enter ) .and. _oEnterCommand != null ;
    .and. _oEnterCommand:CanExecute( oSender )
    _oEnterCommand:Execute( oSender )
  endif
	
  return

end class
and the assignment of the EnterCommand in the ViewModel is simple:

Code: Select all

self:EnterTextBoxCommand := RelayCommand{ TextBoxEnter }
Hope this helps someone!

MVVM: how to build a control with databinding or enter key in a TextBox

Posted: Tue Jun 20, 2017 12:52 pm
by Meinhard
Hi Wolfgang,

just curios. Wouldn't it be better to implement this as an attached property? Thta way you are not in the need to inherit from a control class and can attach they very same solution to different controls?

Regards
Meinhard

MVVM: how to build a control with databinding or enter key in a TextBox

Posted: Tue Jun 20, 2017 1:51 pm
by wriedmann
Hi Meinhard,

yes, that would be another possibility. As often, there are more than only one way to a target....

I have written just a few (databound) controls for my DoorConfigurator, so this is only a stripped down sample to be easier to understand.
And I prefer just code to XAML, but that is a personal preference, because I think I have a better control over the behavior.

Wolfgang

MVVM: how to build a control with databinding or enter key in a TextBox

Posted: Tue Jun 20, 2017 2:00 pm
by Meinhard
> And I prefer just code to XAML,

I know :), but you should be able to use attached dependency properties from code as well.

Regards
Meinhard

MVVM: how to build a control with databinding or enter key in a TextBox

Posted: Tue Jun 20, 2017 2:15 pm
by wriedmann
Hi Meinhard,

I'll research and post another sample when I have a bit of time - have only to finish something now (in X#).

Wolfgang