"updatesourcetrigger=propertychanged and converter" Code Answer

3

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)

i've created a customcontrol that adds functionality to the textbox.

it provided an event handler for the lostfocus event when this event occurs, my converter is called.

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.

of course i could also make a "plustextbox", but i use different converters and i wanted a generic solution.

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;
            }
        }
    }
}
By griffinc on August 25 2022

Answers related to “updatesourcetrigger=propertychanged and converter”

Only authorized users can answer the Search term. Please sign in first, or register a free account.