关于数据绑定

xxaabb 2015-04-26 01:03:36
TextBox控件绑定数据源,没有实现INotifyPropertyChanged接口的数据源照道理在UI界面上面输入,不能更改源数据值。
但我做实验却发现能改了。

实验:2个数据源,一个实现了INotifyPropertyChanged接口的class,一个是简单的class
2个TextBox分别被绑定在2个上述两个数据源

结果:都能通过TextBox输入改变源数据。

问题:为什么?
程序如下:


using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.ComponentModel;

namespace 学习WPF数据绑定 {
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window {
private NewTestData ntd;
private OldTestData otd;

public MainWindow() {
InitializeComponent();

ntd = new NewTestData() { txt = "新绑定" };
otd = new OldTestData() { txt = "旧绑定" };

//设定绑定
Binding bind1 = new Binding();
bind1.Source = ntd;
bind1.Path = new PropertyPath("txt");
textnew1.SetBinding(TextBox.TextProperty,bind1);

Binding bind2 = new Binding();
bind2.Source = otd;
bind2.Path = new PropertyPath("txt");
textold1.SetBinding(TextBox.TextProperty, bind2);

}


private void Button_Click(object sender, RoutedEventArgs e) {
textnew2.Text = ntd.txt;
textold2.Text = otd.txt;

}
}

//=====数据源class NewTestData
public class NewTestData:INotifyPropertyChanged {
private string _txt;
public string txt {
get { return _txt; }
set {
_txt = value;
if (PropertyChanged != null) {
PropertyChanged.Invoke(this, new PropertyChangedEventArgs("txt"));
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
//=====数据源class OldTestData
public class OldTestData {
public string txt { get; set; }
}
}

-----xmal

<Window x:Class="学习WPF数据绑定.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="150" Width="200">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="35*"/>
<RowDefinition Height="35*"/>
<RowDefinition Height="48*"/>
</Grid.RowDefinitions>
<TextBlock Margin="10,5,163,10" Text="新" Grid.Row="0"/>
<TextBox Name="textnew1" Margin="34,5,87,6" Grid.Row="0"/>
<TextBox Name="textnew2" Margin="110,5,10,6" Grid.Row="0"/>

<TextBlock Margin="10,5,163,10" Text="旧" Grid.Row="1"/>
<TextBox Name="textold1" Margin="34,5,87,6" Grid.Row="1"/>
<TextBox Name="textold2" Margin="110,5,10,6" Grid.Row="1"/>

<Button Content="改变" Grid.Row="2" Margin="56,10" Click="Button_Click"/>
</Grid>
</Window>

...全文
132 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
exception92 2015-04-27
  • 打赏
  • 举报
回复
你不要使用 Binding ,你直接赋值,它永远都不会 通知。
  • 打赏
  • 举报
回复
Binding本身已经实现了相关通知接口……
xxx_jiujiamei 2015-04-26
  • 打赏
  • 举报
回复
关于TextBox控件绑定数据源修改示例 //绑定TextBox控件显示数据库数据 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { try { SqlConnection mycon = new SqlConnection(ConfigurationManager.AppSettings["conStr"]); mycon.Open(); DataSet mydataset = new DataSet(); SqlDataAdapter mydataadapter = new SqlDataAdapter("select * from tb_Admin where id=" + Request["id"],mycon); mydataadapter.Fill(mydataset, "tb_Admin"); DataRowView rowview = mydataset.Tables["tb_Admin"].DefaultView[0]; this.txtName.Text = Convert.ToString(rowview["UserName"]); this.txtPwd.Text = Convert.ToString(rowview["PassWord"]); this.ddlSex.SelectedValue = Convert.ToString(rowview["Sex"]); this.txtRealName.Text = Convert.ToString(rowview["ReallyName"]); this.txtBirthday.Text = Convert.ToString(rowview mycon.Close(); } catch (Exception ex) { Response.Write(ex.Message); }//codego.net/15/1/1/ } } //修改TextBox控件数据 protected void btnUpdate_Click(object sender, EventArgs e) { try { SqlData da = new SqlData(); string P_str_Com = "update tb_Admin set UserName='" + this.txtName.Text + "',PassWord='" + this.txtOKpwd.Text + "'" + ",Sex='" + this.ddlSex.SelectedValue + "',ReallyName='" + this.txtRealName.Text + "'" + " where ID='" + Request["ID"] + "'"; bool add = da.ExceSQL(P_str_Com); if (add == true) { Response.Write("<script language=javascript>alert('修改信息成功!');location='AdminManage.aspx'</script>"); } else { Response.Write("<script language=javascript>alert('修改信息失败!');location='javascript:history.go(-1)'</script>"); } } catch (Exception ex) { Response.Write(ex.Message); } }
Forty2 2015-04-26
  • 打赏
  • 举报
回复
因为你是直接给TextBox赋值,而不是改变数据源。 把Button_Click作如下改动就可以看到效果了:
private void Button_Click(object sender, RoutedEventArgs e)
{
    //textnew2.Text = ntd.txt;
    //textold2.Text = otd.txt;
    ntd.txt = "hello";
    otd.txt = "world";
}

110,569

社区成员

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

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

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