Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Thanks for your answers! I looked into this issue myself a bit futher and came up with the following solution (which I'm not entirely satisfied with, but it works fine)</p> <p>I've created a CustomControl that adds functionality to the TextBox.</p> <p>It provided an event handler for the LostFocus event When this event occurs, my converter is called.</p> <p>However, the way I resolve the Converter is not very satisfying (I take it from the Style that is associated with my TextBox). The Style has a Setter for the Text property. From that setter I can access my Converter.</p> <p>Of course I could also make a "PlusTextBox", but I use different converters and I wanted a generic solution.</p> <pre><code>public class TextBoxEx : TextBox { public TextBoxEx() { AddHandler(LostFocusEvent, new RoutedEventHandler(CallConverter), true); } public Type SourceType { get { return (Type)GetValue(SourceTypeProperty); } set { SetValue(SourceTypeProperty, value); } } // Using a DependencyProperty as the backing store for SourceType. This enables animation, styling, binding, etc... public static readonly DependencyProperty SourceTypeProperty = DependencyProperty.Register("SourceType", typeof(Type), typeof(TextBoxEx), new UIPropertyMetadata(null)); private static void CallConverter(object sender, RoutedEventArgs e) { TextBoxEx textBoxEx = sender as TextBoxEx; if (textBoxEx.Style == null) { return; } if (textBoxEx.SourceType == null) { } foreach (Setter setter in textBoxEx.Style.Setters) { if (setter.Property.ToString() == "Text") { if (! (setter.Value is Binding) ) { return; } Binding binding = setter.Value as Binding; if (binding.Converter == null) { return; } object value = binding.Converter.ConvertBack(textBoxEx.Text, textBoxEx.SourceType, binding.ConverterParameter, System.Globalization.CultureInfo.CurrentCulture); value = binding.Converter.Convert(value, typeof(string), binding.ConverterParameter, System.Globalization.CultureInfo.CurrentCulture); if (!(value is string)) { return; } textBoxEx.Text = (string)value; } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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