views:

160

answers:

1

I don't understand the class TextCompositionEventArgs.

There are members of type string named ControlText,SystemText,Text. Then there is a field TextConmposistion which itself contains the members ControlText, SystemText and Text again and additionally the fields SystemCompositionText and CompositionText.

public class TextCompositionEventArgs : InputEventArgs
{
  ..
  public string ControlText { get; }
  public string SystemText { get; }
  public string Text { get; }
  public TextComposition TextComposition { get; }
}

public class TextComposition : DispatcherObject
{
  ..
  public string CompositionText { get; protected set; }
  public string ControlText { get; protected set; }
  public string SystemCompositionText { get; protected set; }
  public string SystemText { get; protected set; }
  public string Text { get; protected set; }
}

Both Text members seem to contain the text typed with the keyboard, all other fields contain empty strings.

In which way do these fields differ and what are they good for?

+1  A: 

TextCompositionEventArgs deals with changes while composing text, so it has many properties dealing with the text and what specifically is changing, and how you use it depends on what events you're handling.

The basic things to understand:

  • Text: This contains that actual text that caused the event - normally the user's typed text

  • SystemText: This contains system text events, ie: if you hit Alt+letter, you'll see the event here. This is normally keystrokes that wouldn't effect text in a control like a text box.

  • ControlText: This is control text events, ie: if you hit Ctrl+letter, you'll see it here. Similar to SystemText.

Normally, if you're just looking for standard "text" events, you'll just want to look at the "Text" property. For details, see the Input Overview.

Reed Copsey
Do you know why ControlText,SystemText,Text are duplicated/redundant in the TextCompositionEventArgs class?
codymanix
They're not. For example, ControlText is test that happens when CTRL is pressed. Those keystrokes won't show up in text.
Reed Copsey
I edited my question and inserted code so you can see what I mean with duplicated/redundant.
codymanix
Oh, I think I understand. I can't look this up ATM, but from what I remember, I think this was done because the TextComposition is a DispatcherObject (for thread safety in the composition itself), but they decided to duplicate the properties to avoid locking when subscribers aren't on the dispatcher thread. I am not sure, however, why they bothered to include the TextComposition object in the eventargs, though.
Reed Copsey