Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to modify a property of a received WPF routed event arg parameter before resuming its propagation?
    text
    copied!<p>I'm playing with a custom TextBox inheriting from the WPF TextBox, trying to learn about WPF events, so my problem is the following: <b>When the TextBox receives an input, I want it to receive instead a case inverted version of this input</b>. For example, if I type on the key (lowercase) "a", I want the TextBox to print a (uppercase) "A" instead of (lowercase) "a".</p> <p>My (partial) solution is, in my custom TextBox, to intercept the TextInput event by overloading the method <a href="http://msdn.microsoft.com/en-us/library/system.windows.uielement.onpreviewtextinput.aspx" rel="nofollow"><code>OnPreviewTextInput</code></a>. When this method is called, I receive a <a href="http://msdn.microsoft.com/en-us/library/system.windows.input.textcompositioneventargs.aspx" rel="nofollow"><code>TextCompositionEventArgs</code></a> whose <a href="http://msdn.microsoft.com/en-us/library/system.windows.input.textcompositioneventargs.text.aspx" rel="nofollow"><code>Text</code></a> property is "a".</p> <p>So my first reflex would be updating this Text property to "A", as in the following code:</p> <pre><code>protected override void OnPreviewTextInput(TextCompositionEventArgs e) { e.Text = "A" ; base.OnPreviewTextInput(e) ; } </code></pre> <p>The problem is that e.Text is readonly, and that I found no easy way to do that (and I searched, and I tweaked both the <a href="http://msdn.microsoft.com/en-us/library/system.windows.input.textcomposition.aspx" rel="nofollow"><code>TextComposition</code></a> and <a href="http://msdn.microsoft.com/en-us/library/system.windows.input.textcompositioneventargs.aspx" rel="nofollow"><code>TextCompositionEventArgs</code></a>, trying to construct one from zero, copying the data, etc.).</p> <p>Did I miss something obvious? Is there a way to do it?</p> <p><i>P.S.: The other solution was to use the WPF TextBox interface to tweak the result (retrieving the current .Text property, putting the inverted character inside, etc.), but this is not the desired solution as it bypasses completely the routed event generation/modification problem I'm trying to solve)</i></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload