C# 和 .NET 中的数据绑定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/450373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Databinding in C# and .NET
提问by Sakkle
I am pretty new to C# and .NET and I'm strugling a little with the whole concept of databinding. What I am asking for is a quick rundown of the concept, or even better, point me towards sources on the net (preferred) or in print that can help me get my head around the idea.
我对 C# 和 .NET 还很陌生,我对数据绑定的整个概念有点挣扎。我要求的是对概念的快速概述,或者更好的是,将我指向网络(首选)或印刷品上的资源,它们可以帮助我理解这个想法。
EDIT:
编辑:
I do my development in vs2008 and we are using winforms
我在 vs2008 中进行开发,我们正在使用 winforms
采纳答案by Marc Gravell
Well, what architecture are you using? winforms? asp.net? wpf?
那么,您使用的是什么架构?窗体?asp.net?wpf?
The high levelis that if you have objects such as:
该高层是,如果你有对象,如:
public class Person {
public string Name {get;set;}
public DateTime DateOfBirth {get;set;}
}
Then you can get the binding framework to do all the hard work, and you just say what you want bound - for example (winforms):
然后你可以让绑定框架来完成所有的艰苦工作,你只需说出你想要绑定的东西——例如(winforms):
txtName.DataBindings.Add("Text", person, "Name");
This sets the textbox's Text property based on the person's Name, and can update the person's Name when the user changes the text.
这会根据人员的姓名设置文本框的 Text 属性,并且可以在用户更改文本时更新人员的姓名。
Multi-record binding is more complex, and is based on IList
in winforms/wpf, and IEunmerable
in ASP.NET; this allows you to bind multiple records (for example into a grid). If the list offers extra features (sorting, filtering etc, via IBindingList
, IBindingListView
, etc), then more functionality might be available.
多记录绑定比较复杂,基于IList
winforms/wpf,IEunmerable
ASP.NET;这允许您绑定多个记录(例如绑定到网格中)。如果列表中提供额外的功能(排序,筛选等,通过IBindingList
,IBindingListView
等等),那么更多的功能可能是可用的。
Binding also allows "observer" usage - i.e. change notification: if you indirectly change the person's Name, then the textbox gets automatically updated. This relies on events - either of the form public event EventHandler NameChanged;
, or (more commonly now) via the INotifyPropertyChanged
event (allowing one event to notify for multiple properties).
绑定还允许“观察者”使用 - 即更改通知:如果您间接更改人员的姓名,则文本框会自动更新。这依赖于事件 - 形式public event EventHandler NameChanged;
,或者(现在更常见)通过INotifyPropertyChanged
事件(允许一个事件通知多个属性)。
Some lists (such as BindingList<T>
, DataView
) have similar notification loops.
某些列表(例如BindingList<T>
, DataView
)具有类似的通知循环。
回答by Frederik Gheysels
The concept of databinding is quite simple; It allows you to 'bind' the data that is contained in an object to a visual control. That control 'displays' your data. When the user changes the value that is displayed by the control, the changes are automatically persisted to the underlying object. Vice versa, when someone changes the data in the object, the control can display the newest value.
数据绑定的概念非常简单。它允许您将对象中包含的数据“绑定”到可视控件。该控件“显示”您的数据。当用户更改控件显示的值时,更改会自动保留到底层对象。反之亦然,当有人更改对象中的数据时,控件可以显示最新值。
http://msdn.microsoft.com/en-us/library/ms752347.aspxhttp://www.akadia.com/services/dotnet_databinding.html
http://msdn.microsoft.com/en-us/library/ms752347.aspx http://www.akadia.com/services/dotnet_databinding.html