microsoft / microsoft-ui-xaml

Windows UI Library: the latest Windows 10 native controls and Fluent styles for your applications
MIT License
6.28k stars 674 forks source link

Proposal: WinUI Controls should have an event like PreviewTextInput from WPF #6471

Open hassaanmasood opened 2 years ago

hassaanmasood commented 2 years ago

Coming from WPF, I am finding it nearly impossible to filter out characters on TextBoxes or Editable ComboBoxes in WinUI Controls. There are no specific events available in WinUI.

In WPF, we can easily filter out unwanted characters using PreviewTextInput event. The nearest event available in WinUI is PreviewKeyDown but 'Microsoft.UI.Xaml.Input.KeyRoutedEventArgs' argument uses Windows.System.VirtualKey enum which does not have mapping for all the keys and hence if you try to cast these enums to chars, many keys are cast to the wrong value and the filtering does not work as intended.

I am specifically referring to WinUI 3 in desktop apps.

kmgallahan commented 2 years ago

https://docs.microsoft.com/en-us/windows/communitytoolkit/extensions/textboxextensions#text-mask

hassaanmasood commented 2 years ago

@kmgallahan I already had a look at the link you mentioned. But my problem actually is more specific to an editable combobox. These textbox extensions do not work on editable comboboxes. One thing I forgot to mention is I specifically faced this issue with WinUI3 in desktop apps not UWP. Not sure if there's already a workaround in them

StephenLPeters commented 2 years ago

@MikeHillberg Is there a different way of doing this for editable combo box?

MikeHillberg commented 2 years ago

Can you use the TextBox.BeforeTextChanging event? This is intended for text filtering scenarios.