c# - WPF: How to bind the result of an OpenFileDialog to a TextBox.Text, which is already bound -


i have listbox, bound list of objects , in datatemplate of listbox have textbox, bound property of objects. have button in datatemplate, too, opens openfiledialog. want bind result of openfiledialog textbox.text, result shown in textbox , value of object, bound textbox changes result.

the xaml:

<listbox name="mylist">     <listbox.itemtemplate>         <datatemplate>             <dockpanel>                 <button name="btnopen" click="btnopen_onclick"/>                 <textbox name="txtpath" text="{binding path=prop2, mode=twoway}"/>             </dockpanel>         </datatemplate>     </listbox.itemtemplate> </listbox> 

the code behind:

private void btnopen_onclick(object sender, routedeventargs e) {     openfiledialog filedialog = new openfiledialog();     filedialog.multiselect = false;      dynamic result = filedialog.showdialog();      if (result == true)      {         //bind textbox textproperty here     } } 

the objects in list bound listbox, structured follows:

public class item {     public string prop1 { get; set; }     public string prop2 { get; set; }     public bool prop3 { get; set; }      public item(string prop1)     {         this.prop1 = prop1;     }      public item(string prop1, string prop2)     {         this.prop1 = prop1;         this.prop2 = prop2;     }      public item(string prop1, string prop2, bool prop3)     {         this.prop1 = prop1;         this.prop2 = prop2;         this.prop3 = prop3;     } } 

your class should implement inofifypropertychanged , collection should implement ilistchanged interface (like observablecollection or bindinglist

if that's case , update property bound control update content.

there many ways implement inotifypropertychanged. quickest solution this:

public class item : inotifypropertychanged {     private string prop2;     public string prop2      {           { return prop2; }          set { prop2 =  value; onpropertychanged("prop2"); }     }      public event propertychangedeventhandler propertychanged;     protected virtual void onpropertychanged(string propertyname)     {         var eh = this.propertychanged;         if (eh != null)             eh(this, new propertychangedeventargs(propertyname));     }  } 

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -