简体   繁体   English

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

[英]How to bind control's two properties to two object properties properly

I have a Form with TextBox on it like this: 我有一个带有TextBox的窗体,如下所示:

        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();

And Testus class like this: 和Testus类是这样的:

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()
    {

    }
}

As you can see, I bind my TextBox to Testus class. 如您所见,我将TextBox绑定到Testus类。 Specifically I bind TextBox.Left to Testus.X and TextBox.Text to Testus.Test. 具体来说,我将TextBox.Left绑定到Testus.X,将TextBox.Text绑定到Testus.Test。 I would like to acomplish that changing Controls Left value will affect Testus.X value and in reverse. 我想补充一下,更改Controls Left值会影响Testus.X值,反之亦然。 And the same for TextBox.Text vs Testus.Test. 与TextBox.Text和Testus.Test相同。

I've added handlers for Click and LostFocus of my TextBox control like this: 我为TextBox控件的Click和LostFocus添加了处理程序,如下所示:

    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;
    }

And I do this test: 我做这个测试:

  1. run application 运行应用程序
  2. enter text into TextBox 在TextBox中输入文字
  3. Change focus to other control 将焦点转移到其他控件

And I get this results in Console: 我在控制台中得到以下结果:

TextBox lost focus

And thats it! 就是这样! Testus.Test does not change it's value!? Testus.Test不会改变它的价值!?

But when I do this: 但是当我这样做时:

  1. run application 运行应用程序
  2. click on textbox (change left value) 单击文本框(更改左值)

I get this results: 我得到以下结果:

Moving to right...
Acomplished: X changed

It seems that Binding Left to X works. 似乎将“绑定到X”起作用。 And Text to Test not. 和文本不测试。 And when I will change places of bindings to this: 当我将绑定的位置更改为此时:

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

Than Text Binding works and left to X binding does not. 比文本绑定有效,而X绑定无效。 So in conclusion: only first DataBinding works. 因此,总而言之:只有第一个DataBinding起作用。

So my quesion is: how to binding two properties of TextBox (Left, Text) to two properties of my object (Testus) (X, Test) so that it will work good? 所以我的问题是:如何将TextBox的两个属性(左,文本)绑定到我的对象(Testus)的两个属性(X,Test),以便其正常工作?

I've always done it like this. 我一直这样做。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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