在 WPF 中将对象绑定到窗口文本框
我有一个名为 Data
的类,其中包含一些公共成员:Name
、Age
、Address
。
我还有带有文本框 Name
、Age
、Address
的窗口。
Data
对象可以随时更改。
如何将 Data
对象绑定到文本框并跟踪对象更改?
我知道有 INotifyPropertyChanged
和“依赖属性”,但我不知道如何使用它们。
编辑
public class MyData : INotifyPropertyChanged
{
private string _name;
public string Name
{
get
{
return _name;
}
set
{
if (_name != value)
{
_name = value;
OnPropertyChnged("Name");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
ProppertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(name));
}
}
XAML 代码:
xmlns:myApp="clr-namespace:MyApp"
<Window.Resources><myApp:MyData x:key = data/></WindowResources>
<TextBox><TextBox.Text><Binding Source="{StaticResource data}" Path="Name" UpdateSourceTrigger="PropertyChanged"/></TextBox.Text></TextBox>
class OtherClass
{
private MyData data;
//the window that have the binding textbox
private MyWindow window;
public OtherClass()
{
data = new MyData();
data.Name = "new name"
window = new MyWindow();
window.show();
}
}
I have a class named Data
with some public members: Name
, Age
, Address
.
I have also window with text boxes Name
, Age
, Address
.
The Data
object can change any time.
How can I bind the Data
object to the text boxes and follow after object changes?
I know there is INotifyPropertyChanged
and "dependency-properties" but I do not know how to use them.
Edit
public class MyData : INotifyPropertyChanged
{
private string _name;
public string Name
{
get
{
return _name;
}
set
{
if (_name != value)
{
_name = value;
OnPropertyChnged("Name");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
ProppertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(name));
}
}
XAML code:
xmlns:myApp="clr-namespace:MyApp"
<Window.Resources><myApp:MyData x:key = data/></WindowResources>
<TextBox><TextBox.Text><Binding Source="{StaticResource data}" Path="Name" UpdateSourceTrigger="PropertyChanged"/></TextBox.Text></TextBox>
class OtherClass
{
private MyData data;
//the window that have the binding textbox
private MyWindow window;
public OtherClass()
{
data = new MyData();
data.Name = "new name"
window = new MyWindow();
window.show();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自 MSDN 的链接很好地解释了这一点。MSDN 链接已失效,添加链接到类似的 文章。
当您的类属性更改时,您的属性应引发带有属性名称的 OnPropertyChanged 事件,以便视图知道刷新它的绑定。
您的文本框应该有一个绑定,例如:
我有一个 ViewModelBase 类,我在其中实现了 OnPropertyChandedEvent 以便所有派生模型调用:
This link from MSDN explains it well.MSDN link is dead, adding link to a similar article.
When your class property is changed, your property should raise a OnPropertyChanged event with the name of the property so that the View knows to refresh it's binding.
And your textbox should have a binding such as:
I have a ViewModelBase class which is where I have implemented my OnPropertyChandedEvent for all derived models to call:
让 Data 类实现 INotifyPropertyChanged 。当有人更改数据实例上的属性值时引发该事件。然后为您的 UI 设置正确的 DataContext,并绑定单个 ui 元素,例如:
Let's the Data class implements INotifyPropertyChanged . Raise the event when someone change the property value on the instances of Data. Then set the proper DataContext to your UI, and bind the single ui element as for example: