刚开始学WPF,现看到绑定,在对一个textbox的text和类的一个字段的绑定遇到了一个问题,界面就是一个textbox和一个buttom
stu.Name改变可以textbox1.text,但是textbox1.text改变不可以改变stu.Name,求解namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private Student stu;
public MainWindow()
{
InitializeComponent();
stu = new Student(); Binding binding = new Binding();
binding.Source = stu;
binding.Path = new PropertyPath("Name"); BindingOperations.SetBinding(this.textBox1, TextBox.TextProperty, binding);
} private void button1_Click(object sender, RoutedEventArgs e)
{
stu.Name += "Name";
}
}
class Student
{
private string name;
public string Name { get { return name; } set { name = value; } }
}
}
stu.Name改变可以textbox1.text,但是textbox1.text改变不可以改变stu.Name,求解namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private Student stu;
public MainWindow()
{
InitializeComponent();
stu = new Student(); Binding binding = new Binding();
binding.Source = stu;
binding.Path = new PropertyPath("Name"); BindingOperations.SetBinding(this.textBox1, TextBox.TextProperty, binding);
} private void button1_Click(object sender, RoutedEventArgs e)
{
stu.Name += "Name";
}
}
class Student
{
private string name;
public string Name { get { return name; } set { name = value; } }
}
}
解决方案 »
- window.opener.location.href 与window.opener.location区别
- 求一个矩阵M*N中的矩形个数?
- 请大家推荐一款C/S模式下使用的报表控件吧
- 统计一下大家用.net做C/S模式开发都用什么报表控件
- 今天给客户安装C# winform系统,运行报错,大家帮忙看看,application has generated an exception that could not be handled
- 关于this
- 关于反射动态中数据类型的问题,在线等...
- (高分求教)如何在客户端检查TextBox的text改变事件,并且调用服务器端的方法来处理。
- 如何用OpenSmtp实现发送Html格式的邮件啊?
- console和winform的区别问题
- winform后台运行的程序用timer会夺取鼠标焦点,请求帮助
- 静态和非静态的dbhelper有什么区别
1. 你要继承INotifyPropertyChanged然后实现接口方法:
#region INotifyPropertyChanged Members
private void NotifyPropertyChanged(string Propertyname)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (PropertyChanged != null)
{
foreach(PropertyChangedEventHandler e in handler.GetInvocationList())
e(this, new PropertyChangedEventArgs(Propertyname));
}
} public event PropertyChangedEventHandler PropertyChanged;
#endregion2. 在你要绑定的property这样做
private string _myProperty;
public string myProperty
{
get
{
return _myProperty;
}
set
{
_myProperty= value;
NotifyPropertyChanged("myProperty");
}
}3.然后在页面上要设值的地方这样绑定
Scr="{Binding myProperty, Mode=Twoway}"