c# - The databinding of a usercontrol doesn't update the source model -


i'm trying implement custom textbox has placeholder text. content of 'firstname' property of model appears in textbox, intended. problem i'm having when change text of textbox, isn't updated in source model. why that?

i've tried setting binding mode "twoway", doesn't change anything. there i'm doing wrong?

edit: silly me! turns out, had put mode="twoway" on both bindings, not usercontrol's. i'll mark answered possible.

model.cs

public class student {     public string firstname { get; set; } } 

mainwindow.xaml

<grid>     <ui:prettytextbox text="{binding firstname}" placeholdertext="#enter name"> </grid> 

prettytextbox.xaml

<usercontrol x:name="prettytextbox">     <grid>         <textblock text="{binding path=placeholdertext, elementname=prettytextbox}"                        visibility="{binding path=text, elementname=prettytextbox, converter={staticresource stringlengthtovisibilityconverter}}"/>         <textbox text="{binding path=text, elementname=prettytextbox, mode=twoway, updatesourcetrigger=propertychanged}"/>     </grid> </usercontrol> 

prettytextbox.xaml.cs

public partial class prettytextbox : inotifypropertychanged {     public static readonly dependencyproperty placeholdertextproperty =             dependencyproperty.register("placeholdertext", typeof (string),                                         typeof(prettytextbox), new frameworkpropertymetadata(default(string)));      public string placeholdertext     {         { return (string)getvalue(placeholdertextproperty); }         set         {             setvalue(placeholdertextproperty, value);             onpropertychanged();         }     }       public static readonly dependencyproperty textproperty =         dependencyproperty.register("text", typeof(string),                                     typeof(prettytextbox), new frameworkpropertymetadata(default(string)));      public string text     {         { return (string)getvalue(textproperty); }         set         {             setvalue(textproperty, value);             onpropertychanged();         }     }      public prettytextbox()     {         initializecomponent();     }  } 

}

you forgot make text property bind 2 way default, need change part:

<ui:prettytextbox text="{binding firstname, mode=twoway, updatesourcetrigger=propertychanged}"/> 

or change frameworkpropertymetadata of text property to:

new frameworkpropertymetadata {     defaultvalue = null,     bindstwowaybydefault = true } 

Comments