c# - Two-way binding to a "custom" RichEditBox's custom properties in ViewModel (UWP) -


in uwp apps, richeditbox control doesn't work on own mvvm-based design patterns when need pass text to/from in viewmodel, created custom version of it.

in custom richeditbox have custom dependencyproperty called text. goal two-way bind property string property located in viewmodel. way can use string in viewmodel set/get text in custom richeditbox. works ease if take xaml-approach. how can done in code-behind file, though? tried , failed pull off. thanks.

in custom richeditbox have custom dependencyproperty called text. goal two-way bind property string property located in viewmodel. way can use string in viewmodel set/get text in custom richeditbox.

for scenario extend customtext property custom richeditbox. know can or set string richeditbox document.settext , document.gettext method. , listen text change of richeditbox textchanged event. have created customricheditbox two-way binding customtext property. please reference following code.

public string customtext   {       { return (string)getvalue(customtextproperty); }       set       {           setvalue(customtextproperty, value);       }   }    public static readonly dependencyproperty customtextproperty =       dependencyproperty.register("customtext", typeof(string), typeof(customricheditbox), new propertymetadata(null, new propertychangedcallback(oncustomtextchanged)));    private static void oncustomtextchanged(dependencyobject d, dependencypropertychangedeventargs e)   {       customricheditbox rich = d customricheditbox;       if (e.newvalue != e.oldvalue)         {             rich.document.settext(windows.ui.text.textsetoptions.none, e.newvalue.tostring());         }   } 

monitor changes of richeditbox text dynamically modify view-model.

public customricheditbox() {     this.defaultstylekey = typeof(richeditbox);     this.textchanged += customricheditbox_textchanged; }  private void customricheditbox_textchanged(object sender, routedeventargs e) {     string value = string.empty;     this.document.gettext(windows.ui.text.textgetoptions.adjustcrlf, out value);     if (string.isnullorempty(value))     {         return;     }     customtext = value; } 

if want bind viewmodel control code-behind file reference following code.

binding mybinding = new binding();  mybinding.source = this.datacontext;  mybinding.path = new propertypath("info");  mybinding.mode = bindingmode.twoway;  mybinding.updatesourcetrigger = updatesourcetrigger.propertychanged;  bindingoperations.setbinding(myeditbox, customricheditbox.customtextproperty, mybinding); 

i have uploaded code sample github. please check.


Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -