新手请教TextBox的Text属性绑定问题

thy38 2012-01-20 10:15:58
我是个C#新手,最近有个程序要把TextBox的Text属性与一个int属性绑定,发现一个奇怪的事,在一个属性中更改另一个属性,TextBox控件显示的值就可以自动更新,但用点击按钮的方法就无效。请看代码:

用于演示的类Class1:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication3
{
class Class1
{
private int _a = 1;

public int A
{
get { return _a; }
set
{

if (_a != value)
{
_b = value + 1;
_a = value;
}
}
}

private int _b = 2;

public int B
{
get { return _b; }
set { _b = value; }
}
}
}

当A的值改变时,B变为A+1。

界面:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
private Class1 _class1 = new Class1();
private TextBox tbA;
private TextBox tbB;
private Button button1;

public Form1()
{
tbA = new TextBox(); //与A绑定
tbB = new TextBox(); //与B绑定
button1 = new Button();

tbA.Location = new Point(105, 39);
tbA.Name = "tbA";
tbA.Size = new Size(121, 21);

tbB.Location = new Point(106, 118);
tbB.Name = "tbB";
tbB.Size = new Size(119, 21);

button1.Location = new Point(148, 67);
button1.Name = "button1";
button1.Size = new Size(23, 45);
button1.TabIndex = 2;
button1.Text = "B加1";
button1.Click += new System.EventHandler(button1_Click);

this.Controls.Add(this.button1);
this.Controls.Add(this.tbB);
this.Controls.Add(this.tbA);

tbA.DataBindings.Add("Text", _class1, "A");
tbB.DataBindings.Add("Text", _class1, "B");
}

private void button1_Click(object sender, EventArgs e)
{
_class1.B += 1;
}
}
}

在tbA中改变数值后按tab,tbB中的数值会自动更新。但点button1后B的值实际上是改变的,但控件中不会更新,除非用INotifyPropertyChanged。请问原因在哪里?
...全文
232 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
huangwenquan123 2012-01-21
  • 打赏
  • 举报
回复
你只是更改了B属性的值,而没有通知客户端B属性值已经更改
 class Class1 : INotifyPropertyChanged
{
private int _a = 1;

public int A
{
get { return _a; }
set
{

if (_a != value)
{
_b = value + 1;
_a = value;
}
}
}

private int _b = 2;

public int B
{
get { return _b; }
set
{
_b = value;
NotifyPropertyChanged("B");
}
}


#region INotifyPropertyChanged 成员

public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string ProName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(ProName));
}
}
#endregion
}

或者你直接给tbB的Text重新赋值。
thy38 2012-01-21
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 yiuke 的回复:]

DataBindings方法绑定是有联动效果的,你要手动赋值就不会
tbA.Text=_class1.A;
[/Quote]

应该就是这个原因,能具体说说什么情况下是联动吗?
thy38 2012-01-21
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 macrojj 的回复:]

没有 INotifyPropertyChanged 相当于单向绑定。

你自己把数据的值改了 用了set当时在Set里面没有通知界面。 哪有这么聪明的
[/Quote]

但为什么在其中一个TextBoxA中修改B的值的时候会更新TextBoxB的显示值呢?大家要多想几个为什么
macrojj 2012-01-21
  • 打赏
  • 举报
回复
没有 INotifyPropertyChanged 相当于单向绑定。

你自己把数据的值改了 用了set当时在Set里面没有通知界面。 哪有这么聪明的
  • 打赏
  • 举报
回复
啥叫 tbB.DataBindings.Add("Text", _class1, "B"),自己好好看看吧。明明没有 INotifyPropertyChanged,怎么更新?
  • 打赏
  • 举报
回复
[Quote=引用楼主 thy38 的回复:]
除非用INotifyPropertyChanged。请问原因在哪里?
[/Quote]

因为你没有用 INotifyPropertyChanged。
Raincen 2012-01-20
  • 打赏
  • 举报
回复
DataBindings方法绑定是有联动效果的,你要手动赋值就不会
tbA.Text=_class1.A;

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧