fbpx
Welcome, Guest
Username: Password: Remember me
Share your code snippets, screen shots etc. here
  • Page:
  • 1

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

MVVM: how to build a control with databinding or enter key in a TextBox 1 year 9 months ago #1

  • wriedmann
  • wriedmann's Avatar Topic Author
  • Offline
  • Posts: 1404
  • Karma: 6
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.
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:
self:EnterTextBoxCommand := RelayCommand{ TextBoxEnter }

Hope this helps someone!
Wolfgang Riedmann
Meran, South Tyrol, Italy
This email address is being protected from spambots. You need JavaScript enabled to view it.
www.riedmann.it - docs.xsharp.it
Attachments:

Please Log in or Create an account to join the conversation.

MVVM: how to build a control with databinding or enter key in a TextBox 1 year 8 months ago #2

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

Please Log in or Create an account to join the conversation.

MVVM: how to build a control with databinding or enter key in a TextBox 1 year 8 months ago #3

  • wriedmann
  • wriedmann's Avatar Topic Author
  • Offline
  • Posts: 1404
  • Karma: 6
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
Wolfgang Riedmann
Meran, South Tyrol, Italy
This email address is being protected from spambots. You need JavaScript enabled to view it.
www.riedmann.it - docs.xsharp.it

Please Log in or Create an account to join the conversation.

MVVM: how to build a control with databinding or enter key in a TextBox 1 year 8 months ago #4

> 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

Please Log in or Create an account to join the conversation.

MVVM: how to build a control with databinding or enter key in a TextBox 1 year 8 months ago #5

  • wriedmann
  • wriedmann's Avatar Topic Author
  • Offline
  • Posts: 1404
  • Karma: 6
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
Wolfgang Riedmann
Meran, South Tyrol, Italy
This email address is being protected from spambots. You need JavaScript enabled to view it.
www.riedmann.it - docs.xsharp.it

Please Log in or Create an account to join the conversation.

  • Page:
  • 1