C# 如何将控件的两个属性正确绑定到两个对象属性

C# 如何将控件的两个属性正确绑定到两个对象属性,c#,winforms,data-binding,binding,C#,Winforms,Data Binding,Binding,我有一个上面有文本框的表单,如下所示: Form f = new Form(); TextBox t = new TextBox (); t.Click += new EventHandler(t_Click); t.LostFocus += new EventHandler(t_LostFocus); Testus tt = new Testus(); t.DataBindings.Add("L

我有一个上面有文本框的表单,如下所示:

        Form f = new Form();
        TextBox t = new TextBox ();
        t.Click += new EventHandler(t_Click);
        t.LostFocus += new EventHandler(t_LostFocus);

        Testus tt = new Testus();

        t.DataBindings.Add("Left", Testus , "X");
        t.DataBindings.Add("Text", Testus , "Test");

        f.Controls.Add(t);
        f.ShowDialog();
class Testus
{
    public string Test
    {
        get
        {
            return _text;
        }
        set
        {
            Console.WriteLine("Acomplished: text change");
            _text = value;
        }
    }
    private string _text;

    public int X
    {
        get
        {
            return x;
        }
        set
        {
            Console.WriteLine("Acomplished: X changed");
            x = value;
        }
    }
    int x;

    public Testus()
    {

    }
}
    static void t_LostFocus(object sender, EventArgs e)
    {
        Console.WriteLine("TextBox lost focus");
    }

    static void t_Click(object sender, EventArgs e)
    {
        Console.WriteLine("Moving to right...");
        ((Control)sender).Left = 100;
    }
Testus类如下所示:

        Form f = new Form();
        TextBox t = new TextBox ();
        t.Click += new EventHandler(t_Click);
        t.LostFocus += new EventHandler(t_LostFocus);

        Testus tt = new Testus();

        t.DataBindings.Add("Left", Testus , "X");
        t.DataBindings.Add("Text", Testus , "Test");

        f.Controls.Add(t);
        f.ShowDialog();
class Testus
{
    public string Test
    {
        get
        {
            return _text;
        }
        set
        {
            Console.WriteLine("Acomplished: text change");
            _text = value;
        }
    }
    private string _text;

    public int X
    {
        get
        {
            return x;
        }
        set
        {
            Console.WriteLine("Acomplished: X changed");
            x = value;
        }
    }
    int x;

    public Testus()
    {

    }
}
    static void t_LostFocus(object sender, EventArgs e)
    {
        Console.WriteLine("TextBox lost focus");
    }

    static void t_Click(object sender, EventArgs e)
    {
        Console.WriteLine("Moving to right...");
        ((Control)sender).Left = 100;
    }
如您所见,我将文本框绑定到Testus类。具体来说,我将TextBox.Left绑定到Testus.X,将TextBox.Text绑定到Testus.Test。我想证实更改控件的左值将影响Testus.X值,反之亦然。对于TextBox.Text和Testus.Test也一样

我已经为文本框控件的单击和丢失焦点添加了如下处理程序:

        Form f = new Form();
        TextBox t = new TextBox ();
        t.Click += new EventHandler(t_Click);
        t.LostFocus += new EventHandler(t_LostFocus);

        Testus tt = new Testus();

        t.DataBindings.Add("Left", Testus , "X");
        t.DataBindings.Add("Text", Testus , "Test");

        f.Controls.Add(t);
        f.ShowDialog();
class Testus
{
    public string Test
    {
        get
        {
            return _text;
        }
        set
        {
            Console.WriteLine("Acomplished: text change");
            _text = value;
        }
    }
    private string _text;

    public int X
    {
        get
        {
            return x;
        }
        set
        {
            Console.WriteLine("Acomplished: X changed");
            x = value;
        }
    }
    int x;

    public Testus()
    {

    }
}
    static void t_LostFocus(object sender, EventArgs e)
    {
        Console.WriteLine("TextBox lost focus");
    }

    static void t_Click(object sender, EventArgs e)
    {
        Console.WriteLine("Moving to right...");
        ((Control)sender).Left = 100;
    }
我做了这个测试:

运行应用程序 在文本框中输入文本 将焦点更改为其他控件 我在控制台中得到这个结果:

TextBox lost focus
就这样!Testus.Test不会改变它的值

但当我这么做的时候:

运行应用程序 单击文本框更改左值 我得到了这样的结果:

Moving to right...
Acomplished: X changed
似乎将左键绑定到X是可行的。和文本不测试。当我将更改此的绑定位置时:

    t.DataBindings.Add("Text", Testus , "Test");
    t.DataBindings.Add("Left", Testus , "X");
比文本绑定更有效,而留给X绑定则不行。总之,只有第一次数据绑定有效


所以我的问题是:如何绑定TextBox Left的两个属性,将文本绑定到我的对象Testus X的两个属性,进行测试以使其正常工作?

我一直这样做

Binding b = new Binding("Test");  
b.Source = tt;  
t.SetBinding(TextBox.TextProperty, b);