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
Code: Select all
self:EnterTextBoxCommand := RelayCommand{ TextBoxEnter }