简体   繁体   中英

Binding properties to controls using reflection

I am trying to bind properties from object using reflection to textbox, object passed in is simple class with public primitive type properties and some code in getter/setter. But changing values in textbox doesn't reflect changes to instance of object, values never gets updated, what am I missing?

public object myObj;

public void setObject(ref object myObject)
{
    myObj = myObject;
}

var textbox = new Textbox();
...
textbox.DataBindings.Add("Text", myObj, myObj.GetType().GetProperties()[0].Name);
this.Controls.Add(textbox);

Check this post please.

You need to have:

  Binding myBinding = new Binding("MyDataProperty"); //name of the property on the object which is used as binding source
  myBinding.Source = myObject;
  textbox.SetBinding(TextBlock.TextProperty, myBinding);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM