HierarchicalDataBoundControl.PerformDataBinding 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在派生类中重写时,将数据源中的数据绑定到控件。
protected public:
virtual void PerformDataBinding();
protected internal virtual void PerformDataBinding ();
abstract member PerformDataBinding : unit -> unit
override this.PerformDataBinding : unit -> unit
Protected Friend Overridable Sub PerformDataBinding ()
示例
下面的代码示例演示如何在派生自 HierarchicalDataBoundControl的类中实现 PerformDataBinding 方法。 控件GeneologyTree
循环访问IHierarchicalEnumerable从其关联的 HierarchicalDataSourceView检索到的 和 IHierarchyData 对象,并为它绑定到的数据创建文本树结构。 此代码示例是为 HierarchicalDataBoundControl 类提供的一个更大示例的一部分。
protected override void PerformDataBinding() {
base.PerformDataBinding();
// Do not attempt to bind data if there is no
// data source set.
if (!IsBoundUsingDataSourceID && (DataSource == null)) {
return;
}
HierarchicalDataSourceView view = GetData(RootNode.DataPath);
if (view == null) {
throw new InvalidOperationException
("No view returned by data source control.");
}
IHierarchicalEnumerable enumerable = view.Select();
if (enumerable != null) {
Nodes.Clear();
try {
RecurseDataBindInternal(RootNode, enumerable, 1);
}
finally {
}
}
}
private void RecurseDataBindInternal(TreeNode node,
IHierarchicalEnumerable enumerable, int depth) {
foreach(object item in enumerable) {
IHierarchyData data = enumerable.GetHierarchyData(item);
if (null != data) {
// Create an object that represents the bound data
// to the control.
TreeNode newNode = new TreeNode();
RootViewNode rvnode = new RootViewNode();
rvnode.Node = newNode;
rvnode.Depth = depth;
// The dataItem is not just a string, but potentially
// an XML node or some other container.
// If DataTextField is set, use it to determine which
// field to render. Otherwise, use the first field.
if (DataTextField.Length > 0) {
newNode.Text = DataBinder.GetPropertyValue
(data, DataTextField, null);
}
else {
PropertyDescriptorCollection props =
TypeDescriptor.GetProperties(data);
// Set the "default" value of the node.
newNode.Text = String.Empty;
// Set the true data-bound value of the TextBox,
// if possible.
if (props.Count >= 1) {
if (null != props[0].GetValue(data)) {
newNode.Text =
props[0].GetValue(data).ToString();
}
}
}
Nodes.Add(rvnode);
if (data.HasChildren) {
IHierarchicalEnumerable newEnumerable =
data.GetChildren();
if (newEnumerable != null) {
RecurseDataBindInternal(newNode,
newEnumerable, depth+1 );
}
}
if ( _maxDepth < depth) _maxDepth = depth;
}
}
}
Protected Overrides Sub PerformDataBinding()
MyBase.PerformDataBinding()
' Do not attempt to bind data if there is no
' data source set.
If Not IsBoundUsingDataSourceID AndAlso DataSource Is Nothing Then
Return
End If
Dim view As HierarchicalDataSourceView = GetData(RootNode.DataPath)
If view Is Nothing Then
Throw New InvalidOperationException _
("No view returned by data source control.")
End If
Dim enumerable As IHierarchicalEnumerable = view.Select()
If Not (enumerable Is Nothing) Then
Nodes.Clear()
Try
RecurseDataBindInternal(RootNode, enumerable, 1)
Finally
End Try
End If
End Sub
Private Sub RecurseDataBindInternal(ByVal node As TreeNode, _
ByVal enumerable As IHierarchicalEnumerable, _
ByVal depth As Integer)
Dim item As Object
For Each item In enumerable
Dim data As IHierarchyData = enumerable.GetHierarchyData(item)
If Not data Is Nothing Then
' Create an object that represents the bound data
' to the control.
Dim newNode As New TreeNode()
Dim rvnode As New RootViewNode()
rvnode.Node = newNode
rvnode.Depth = depth
' The dataItem is not just a string, but potentially
' an XML node or some other container.
' If DataTextField is set, use it to determine which
' field to render. Otherwise, use the first field.
If DataTextField.Length > 0 Then
newNode.Text = DataBinder.GetPropertyValue _
(data, DataTextField, Nothing)
Else
Dim props As PropertyDescriptorCollection = _
TypeDescriptor.GetProperties(data)
' Set the "default" value of the node.
newNode.Text = String.Empty
' Set the true data-bound value of the TextBox,
' if possible.
If props.Count >= 1 Then
If Not props(0).GetValue(data) Is Nothing Then
newNode.Text = props(0).GetValue(data).ToString()
End If
End If
End If
Nodes.Add(rvnode)
If data.HasChildren Then
Dim newEnumerable As IHierarchicalEnumerable = _
data.GetChildren()
If Not (newEnumerable Is Nothing) Then
RecurseDataBindInternal(newNode, _
newEnumerable, depth + 1)
End If
End If
If MaxDepth < depth Then
MaxDepth = depth
End If
End If
Next item
End Sub
注解
从 HierarchicalDataBoundControl 类派生数据绑定控件时,实现此方法而不是 DataBind 方法。 将 控件的数据绑定逻辑置于 中 PerformDataBinding 可防止 DataBinding 以错误的顺序引发 和 DataBound 事件。
虽然基 HierarchicalDataBoundControl 类不提供此方法的特定实现, PerformDataBinding 但 方法将 PerformSelect 调用 方法,以将任何用户界面控件的值绑定到方法 PerformSelect 检索的数据。