IHierarchyData.HasChildren Vlastnost
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Určuje, zda hierarchický datový uzel, který objekt představuje, IHierarchyData má jakékoli podřízené uzly.
public:
property bool HasChildren { bool get(); };
public bool HasChildren { get; }
member this.HasChildren : bool
Public ReadOnly Property HasChildren As Boolean
Hodnota vlastnosti
truepokud má aktuální uzel podřízené uzly; v opačném případě . false
Příklady
Následující příklad kódu ukazuje, jak ASP.NET hierarchický ovládací prvek vázané na data používá IHierarchyData objekt v rekurzivní datové vazbě metoda. Položky v IHierarchicalEnumerable kolekci jsou výčty a pro každý IHierarchyData objekt je načten pomocí GetHierarchyData metody. Nakonec je tato vlastnost zaškrtnutá HasChildren a určí, zda se má provést rekurse. Tento příklad kódu je součástí většího příkladu uvedeného pro třídu HierarchicalDataBoundControl.
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;
}
}
}
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
Poznámky
Vlastnost HasChildren je pohodlná vlastnost, která umožňuje volajícím vyhnout se kontrole návratové GetChildren hodnoty metody pro null. Pokud vlastnost HasChildren vrátí true, zavolejte metodu GetChildren pro načtení IHierarchicalEnumerable kolekce podřízených uzlů.