In winforms, is there a better (or good) way to bind a form (presentation) 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 text fields, called txtFirstName, txtMiddleName,txtLastName

I want to somehow automatically bind a domain object to these text fields.

I am very used to working with asp.net-mvc, but I'm trying to pass this knowledge to winforms 0_0

+3
source share
3 answers

You need a "Data Source", in particular an "object data source".

This will allow you to start with the "Data" menu, select "Add New Data Source ...". You want to select "Object".

http://msdn.microsoft.com/en-us/library/w4dd7z6t(VS.80).aspx.

http://msdn.microsoft.com/en-us/library/5xf878ky.aspx.

+1
Name n = new Name { First = "test", Last = "last", Middle = "midddle" };
        textBox1.DataBindings.Add("Text", n, "First");
+1

I'm not quite sure if this is what you are asking for, but you can override your object's tostring method

public override string ToString()
    {
        return string.Format("first:{0}, middle:{1} last:{2}", First, Middle, Last);
    }

then you can set it as a control data source.

0
source

Source: https://habr.com/ru/post/1731621/


All Articles