I’m using a key_up event on my window to append the data typed to a string builder.
<Grid x:Name="grdMain" KeyUp="grdMain_KeyUp">
<Grid.RowDefinitions>...
private StringBuilder buffer = new StringBuilder();
private void Window_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key != Key.Enter)
{
buffer.Append(e.Key);
}
the thing is that the data applied to the buffer is “NumPad4” instead of “4” and “D3” instead of “3”…
am i missing something? is there a way to append the data as if it was typed into a textbox?
i know that i can convert the data my self but it seems weird that there is no built in way to do so.
Ok, this may seem weird at first glance, but I believe that it’s the right way to do that. I derived from a
TextBoxand addedContentPresenterthere to host any content and theContentproperty to set that content.So, create WPF Custom Control (it’s Visual Studio template) called TextBoxContainer. add the following property to it:
Replace it’s style in Assets/generic.xaml file with the following:
And use it like following:
Note that at any time it has text that contains all text that was input. The only thing that you should be aware of is that some elements prevent some events from bubbling up. For example
TextBoxstopsKeyDownevent bubbling. So if you placeTextBoxinsideTextBoxContainerand user will click on it and start to input text there – TextBoxContainer will not receive that text, although it will fireTextChangedevent.