In winforms what is the best (or a good) way to bind a form (view) to a strongly-typed object?
For example if I have a Name object
public class Name
{
public string First { get; set; }
public string Middle { get; set; }
public string Last { get; set; }
}
and I have a form with 3 textboxes on it, named txtFirstName
, txtMiddleName
, txtLastName
I want some way to automatically bind the domain object to these text boxes.
I'm very used to working with asp.net-mvc but I'开发者_Go百科m trying to transfer this knowledge to winforms 0_0
You want a "Data Source", specifically an "object data source".
This will get you started, from the "Data" menu, select "Add New Data Source..." You want to select "Object".
Data Source Configuration Wizard at http://msdn.microsoft.com/en-us/library/w4dd7z6t(VS.80).aspx.
How to: Connect to Data in an Object at http://msdn.microsoft.com/en-us/library/5xf878ky.aspx.
Name n = new Name { First = "test", Last = "last", Middle = "midddle" };
textBox1.DataBindings.Add("Text", n, "First");
I'm not quite sure if this is what you are asking but, you can override the tostring method of your object
public override string ToString()
{
return string.Format("first:{0}, middle:{1} last:{2}", First, Middle, Last);
}
then you can set it as the datasource of a control.
精彩评论