c# - Refresh Target of all bindings -


i having bit of difficulty in crud form. have button saves form , has isdefault flag set true, user can press enter @ point save form.

the problem when user typing in textbox , hits enter key source of textbox binding isn't updated. know because default updatesourcetrigger functionality textboxes lostfocus, i've used overcome problem cases, causes more problems in other cases.

for standard string fields fine, things doubles , ints validation occurs on property change, , stop user typing 1.5 textbox bound double source (they can type 1, validation stops decimal, type 15 move cursor , press . though).

is there better way approach this? looked @ ways refresh bindings in window in code came firing propertychanged event string.empty refreshes target, not source.

my standard solution when don't want set updatesourcetrigger=propertychanged on bindings use custom attachedproperty when set true will update source of binding when enter pressed.

here's copy of attached property

// when set true, enter key update source #region enterupdatestextsource dependencyproperty  // property determine if enter key should update source. default false public static readonly dependencyproperty enterupdatestextsourceproperty =     dependencyproperty.registerattached("enterupdatestextsource", typeof (bool),                                         typeof (textboxhelper),                                         new propertymetadata(false, enterupdatestextsourcepropertychanged));  // public static bool getenterupdatestextsource(dependencyobject obj) {     return (bool) obj.getvalue(enterupdatestextsourceproperty); }  // set public static void setenterupdatestextsource(dependencyobject obj, bool value) {     obj.setvalue(enterupdatestextsourceproperty, value); }  // changed event - attach previewkeydown handler private static void enterupdatestextsourcepropertychanged(dependencyobject obj,                                                           dependencypropertychangedeventargs e) {     var sender = obj uielement;     if (obj != null)     {         if ((bool) e.newvalue)         {             sender.previewkeydown += onpreviewkeydownupdatesourceifenter;         }         else         {             sender.previewkeydown -= onpreviewkeydownupdatesourceifenter;         }     } }  // if key being pressed enter key, , enterupdatestextsource set true, update source text property private static void onpreviewkeydownupdatesourceifenter(object sender, keyeventargs e) {     if (e.key == key.enter)     {         if (getenterupdatestextsource((dependencyobject) sender))         {             var obj = sender uielement;             bindingexpression textbinding = bindingoperations.getbindingexpression(                 obj, textbox.textproperty);              if (textbinding != null)                 textbinding.updatesource();         }     } }  #endregion //enterupdatestextsource dependencyproperty 

and it's used this:

<textbox text="{binding sometext}" local:enterupdatestextsource="true" /> 

Comments

Popular posts from this blog

Change php variable from jquery value using ajax (same page) -

Pull out data related to my apps from Android Play Store and iOS App Store -

How can I fetch data from a web server in an android application? -