We are writing a WPF based application that is usually used on a touchscreen tablet. We are designing the UI to avoid text input, but there times when that can't be avoided. For those times we want to control when and what type of keyboard is popped up for input.
We can create a base custom control to add the appropriate event handler, but I was wondering if there was a way to do this by convention instead.
We are using CaliburnMicro for our MVVM framework and as it supports convention customization I started looking into ConventionManager.AddElementConvention<TextBox>(null, null, "GotFocus")
but calling that will replace any existing conventions. I thought about something like:
var textboxConvention = ConventionManager.GetElementConvention(typeof(TextBox));
var oldBinding = textboxConvention.ApplyBinding;
textboxConvention.ApplyBinding =
(viewModelType, path, property, element, convention) =>
{
element.GotFocus += ((o, args) => ShowKeyboard((TextBox)o));
element.LostFocus += ((o, args) => HideKeyboard((TextBox)o));
return oldBinding(viewModelType, path, property, element, convention);
};
...but I suspect I've gone down the wrong path here.
Is there a better way to chain bindings? Is there a better way to handling the focus events/bringing up the keyboard?
I've done this before. You can create an attached behavior that listens for PreviewGotKeyboardFocus on the element in question causing the OnScreenKeyboard to display. Then create a style that applies the attached behavior to those controls. In my project, we also had different Keyboards for different input types (for example text versus numeric). The attached behavior set a KeyboardType for its property. Based on the KeyboardType, I would display the appropriate keyboard.
If you need further assistance let me know.