簡體   English   中英

具有兩種方式 DataBinding 的 Winforms 文本框無法正常工作

[英]Winforms Textbox with two way DataBinding not working as it should be

Winforms 有兩個文本框。 textBox2綁定到屬性 Unit。 我希望對 Unit 或textBox2所做的任何更改都會分別自動更新textBox2或 Unit。 但事實並非如此。 以下是 Winform 代碼的三個版本。

版本一設置數據綁定,希望有兩種方式自動更新但是不行

public partial class Receiver : Form, INotifyPropertyChanged
{         

    private int unit=0;
    public int Unit
    {
        get { return unit; }
        set
        {
            if (value != unit)
            {
                unit = value;                                          
            }
        }
    }

    public Receiver()
    {
        InitializeComponent();            
        textBox2.DataBindings.Add("Text", Unit, "Unit");
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {   
        //textBox1 change makes Unit change, 
        //I wish the change will be displayed in textBox2 automatically
        Unit = Convert.ToInt32(textBox1.Text);
    }        
}

帶有事件處理程序的版本 2 ,使用事件處理程序更新textBox2的硬代碼

但更改textBox2仍然不會自動更新 Unit

public partial class Receiver : Form, INotifyPropertyChanged
{ 
    public event PropertyChangedEventHandler PropertyChanged;

    private int unit=0;
    public int Unit
    {
        get { return unit; }
        set
        {
            if (value != unit)
            {
                unit = value;                       
                if (PropertyChanged != null)
                {
                   PropertyChanged(this, new PropertyChangedEventArgs(info));
                }  
            }
        }
    }

    public Receiver()
    {
        InitializeComponent();            
        textBox2.DataBindings.Add("Text", this.Unit, "Unit", false, 
                                                 DataSourceUpdateMode.OnPropertyChanged);
        PropertyChanged += new PropertyChangedEventHandler(OnPropertyChanged);

    }    

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        Unit = Convert.ToInt32(textBox1.Text);
    }


    private void OnPropertyChanged(object sender, EventArgs e)
    {  
        //this actually is hard coded to update textBox2, binding does no help
        textBox2.Text = Unit.ToString();
    }
}

版本三為什么使用事件處理程序,我們可以簡單地這樣做。

public partial class Receiver : Form, INotifyPropertyChanged
{ 
    private int unit=0;
    public int Unit
    {
        get { return unit; }
        set
        {
            if (value != unit)
            {
                unit = value;   
                textBox2.text = unit.toString();                          
            }
        }
    }

    public Receiver()
    {
        InitializeComponent();            
        textBox2.DataBindings.Add("Text", Unit, "Unit");
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        Unit = Convert.ToInt32(textBox1.Text);           
    }   

    private void textBox2_TextChanged(object sender, EventArgs e)
    {
        Unit = Convert.ToInt32(textBox2.Text);           
    } 
}

}

版本二版本三有問題,對 textbox1 的任何更改都會導致textbox1 textbox2 這將導致大量的 CPU 周期。 最好的方法是當鼠標焦點離開textBox1然后進行更新。 那么該怎么做呢?

您的代碼中的問題在於:

textBox2.DataBindings.Add("Text", Unit, "Unit");

您應該將數據源替換為包含該屬性而不是屬性本身的實例,並且您還應該在此處指定數據源更新模式,將其設置為DataSourceUpdateMode.OnPropertyChanged 所以:

textBox2.DataBindings.Add("Text", this, "Unit", false, DataSourceUpdateMode.OnPropertyChanged);

//test using the second or the third approach:
Unit = 15;//now the textBox2.Text equals to 15 too.

textBox2.Text = 12;//now Unit equals 12 too

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM