TreeNodeBinding クラス
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
TreeView コントロール内のデータ項目とバインドするノードの間のリレーションシップを定義します。
public ref class TreeNodeBinding sealed : ICloneable, System::Web::UI::IDataSourceViewSchemaAccessor, System::Web::UI::IStateManager
public sealed class TreeNodeBinding : ICloneable, System.Web.UI.IDataSourceViewSchemaAccessor, System.Web.UI.IStateManager
type TreeNodeBinding = class
interface IStateManager
interface ICloneable
interface IDataSourceViewSchemaAccessor
Public NotInheritable Class TreeNodeBinding
Implements ICloneable, IDataSourceViewSchemaAccessor, IStateManager
- 継承
-
TreeNodeBinding
- 実装
例
次の表は、ツリー ノードバインド宣言の例を示しています。
| バインドの例 | Description |
|---|---|
<asp:TreeNodeBinding TextField="Title" ValueField= "ID"/> |
ツリー内のすべてのノードの Text プロパティと Value プロパティをそれぞれデータ ソースの Title フィールドと ID フィールドにバインドします。
DataMemberプロパティとDepthプロパティが設定されていないため、すべてのノードでこのツリー ノード バインド宣言が使用されます。 |
<asp:TreeNodeBinding DataMember= "Book" TextField= "Title" ValueField= "ID"/> |
ツリー内のすべてのノードのTextプロパティとValue プロパティをそれぞれ、データ ソース内のTitle データ項目のIDフィールドとBook フィールドにバインドします。 |
<asp:TreeNodeBinding Depth="2" TextField= "Title" ValueField= "ID"/> |
ツリーの深さが 2 のすべてのノードの Text プロパティと Value プロパティをそれぞれ、データ ソース内のデータ項目の Title フィールドと ID フィールドにバインドします。 |
<asp:TreeNodeBinding DataMember="Book" Depth= "2" TextField= "Title" ValueField= "ID" ImageUrl= "Image.jpg"> |
ツリーの深さが 2 のすべてのノードのTextプロパティとValue プロパティをそれぞれ、データ ソース内のTitle データ項目のIDフィールドとBookフィールドにバインドします。 また、ノードの ImageUrl プロパティを静的な値にバインドします。 |
このセクションには、3 つのコード例が含まれています。 最初のコード例では、 TreeNodeBinding オブジェクトを宣言によって使用して、ノードとデータ項目の間のリレーションシップを定義する方法を示します。 2 番目のコード例では、 TreeNodeBinding オブジェクトをプログラムで使用して、ノードとデータ項目の間のリレーションシップを定義する方法を示します。 3 番目のコード例では、1 番目と 2 番目のコード例のサンプル XML データを提供します。
次のコード例では、 TreeNodeBinding オブジェクトを宣言によって使用して、ノードとデータ項目の間のリレーションシップを定義する方法を示します。 この例を正しく機能させるには、このコード例の後に提供されるサンプル XML データを、Book.xmlという名前のファイルにコピーする必要があります。
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>TreeView XML Data Binding Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>TreeView XML Data Binding Example</h3>
<asp:TreeView id="BookTreeView"
DataSourceID="BookXmlDataSource"
runat="server">
<DataBindings>
<asp:TreeNodeBinding DataMember="Book" TextField="Title"/>
<asp:TreeNodeBinding DataMember="Chapter" TextField="Heading"/>
<asp:TreeNodeBinding DataMember="Section" TextField="Heading"/>
</DataBindings>
</asp:TreeView>
<asp:XmlDataSource id="BookXmlDataSource"
DataFile="Book.xml"
runat="server">
</asp:XmlDataSource>
</form>
</body>
</html>
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>TreeView XML Data Binding Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>TreeView XML Data Binding Example</h3>
<asp:TreeView id="BookTreeView"
DataSourceID="BookXmlDataSource"
runat="server">
<DataBindings>
<asp:TreeNodeBinding DataMember="Book" TextField="Title"/>
<asp:TreeNodeBinding DataMember="Chapter" TextField="Heading"/>
<asp:TreeNodeBinding DataMember="Section" TextField="Heading"/>
</DataBindings>
</asp:TreeView>
<asp:XmlDataSource id="BookXmlDataSource"
DataFile="Book.xml"
runat="server">
</asp:XmlDataSource>
</form>
</body>
</html>
次のコード例では、 TreeNodeBinding オブジェクトをプログラムで使用して、ノードとデータ項目の間のリレーションシップを定義する方法を示します。 この例を正しく機能させるには、次のコード例で提供されているサンプル XML データを Book.xmlという名前のファイルにコピーする必要があります。
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void Page_Load(Object sender, EventArgs e)
{
// Create a new TreeView control.
TreeView NewTree = new TreeView();
// Set the properties of the TreeView control.
NewTree.ID = "BookTreeView";
NewTree.DataSourceID = "BookXmlDataSource";
// Create the tree node binding relationship.
// Create the root node binding.
TreeNodeBinding RootBinding = new TreeNodeBinding();
RootBinding.DataMember = "Book";
RootBinding.TextField = "Title";
// Create the parent node binding.
TreeNodeBinding ParentBinding = new TreeNodeBinding();
ParentBinding.DataMember = "Chapter";
ParentBinding.TextField = "Heading";
// Create the leaf node binding.
TreeNodeBinding LeafBinding = new TreeNodeBinding();
LeafBinding.DataMember = "Section";
LeafBinding.TextField = "Heading";
// Add bindings to the DataBindings collection.
NewTree.DataBindings.Add(RootBinding);
NewTree.DataBindings.Add(ParentBinding);
NewTree.DataBindings.Add(LeafBinding);
// Manually register the event handler for the SelectedNodeChanged event.
NewTree.SelectedNodeChanged += new EventHandler(this.Node_Change);
// Add the TreeView control to the Controls collection of the PlaceHolder control.
ControlPlaceHolder.Controls.Add(NewTree);
}
void Node_Change(Object sender, EventArgs e)
{
// Retrieve the TreeView control from the Controls collection of the PlaceHolder control.
TreeView LocalTree = (TreeView)ControlPlaceHolder.FindControl("BookTreeView");
// Display the selected node.
Message.Text = "You selected: " + LocalTree.SelectedNode.Text;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>TreeView Constructor Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>TreeView Constructor Example</h3>
<asp:PlaceHolder id="ControlPlaceHolder" runat="server">
</asp:PlaceHolder>
<asp:XmlDataSource id="BookXmlDataSource"
DataFile="Book.xml"
runat="server">
</asp:XmlDataSource>
<br /><br />
<asp:Label id="Message" runat="server"/>
</form>
</body>
</html>
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
' Create a new TreeView control.
Dim NewTree As New TreeView
' Set the properties of the TreeView control.
NewTree.ID = "BookTreeView"
NewTree.DataSourceID = "BookXmlDataSource"
' Create the tree node binding relationship.
' Create the root node binding.
Dim RootBinding As New TreeNodeBinding
RootBinding.DataMember = "Book"
RootBinding.TextField = "Title"
' Create the parent node binding.
Dim ParentBinding As New TreeNodeBinding
ParentBinding.DataMember = "Chapter"
ParentBinding.TextField = "Heading"
' Create the leaf node binding.
Dim LeafBinding As New TreeNodeBinding
LeafBinding.DataMember = "Section"
LeafBinding.TextField = "Heading"
' Add bindings to the DataBindings collection.
NewTree.DataBindings.Add(RootBinding)
NewTree.DataBindings.Add(ParentBinding)
NewTree.DataBindings.Add(LeafBinding)
' Manually register the event handler for the SelectedNodeChanged event.
AddHandler NewTree.SelectedNodeChanged, AddressOf Node_Change
' Add the TreeView control to the Controls collection of the PlaceHolder control.
ControlPlaceHolder.Controls.Add(NewTree)
End Sub
Sub Node_Change(ByVal sender As Object, ByVal e As EventArgs)
' Retrieve the TreeView control from the Controls collection of the PlaceHolder control.
Dim LocalTree As TreeView = CType(ControlPlaceHolder.FindControl("BookTreeView"), TreeView)
' Display the selected node.
Message.Text = "You selected: " & LocalTree.SelectedNode.Text
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>TreeView Constructor Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>TreeView Constructor Example</h3>
<asp:PlaceHolder id="ControlPlaceHolder" runat="server">
</asp:PlaceHolder>
<asp:XmlDataSource id="BookXmlDataSource"
DataFile="Book.xml"
runat="server">
</asp:XmlDataSource>
<br /><br />
<asp:Label id="Message" runat="server"/>
</form>
</body>
</html>
次のコード例では、前のコード例のサンプル XML データを提供します。
<Book Title="Book Title">
<Chapter Heading="Chapter 1">
<Section Heading="Section 1">
</Section>
<Section Heading="Section 2">
</Section>
</Chapter>
<Chapter Heading="Chapter 2">
<Section Heading="Section 1">
</Section>
</Chapter>
</Book>
注釈
各データ項目に複数のフィールド (複数の属性を持つ XML 要素など) が含まれるデータ ソースに TreeView コントロールがバインドされている場合、ノードには、データ項目の ToString メソッドによって返される値が既定で表示されます。 XML 要素の場合、ノードには要素名が表示されます。この名前はツリーの基になる構造を示しますが、それ以外の場合はあまり役に立ちません。 ツリー ノードのバインドを指定することで、ノードのプロパティを特定のフィールドにバインドできます。
TreeNodeBinding オブジェクトは、各データ項目とバインドするノードの間のリレーションシップを定義します。
TreeView コントロールは、TreeNodeBinding プロパティにそのDataBindings オブジェクトを格納し、バインドをデータ ソースに適用して、ツリー階層とデータ ソース階層の間に 1 対 1 のリレーションシップを作成します。 データ ソース内のデータ項目ごとに、TreeView コントロールは、対応するTreeNodeBinding オブジェクトを作成するために、データ項目をTreeNode オブジェクトと照合しようとします。
TreeNodeBinding オブジェクトを作成するときは、バインドの条件を指定する必要があります。 条件は、データ項目をノードにバインドする必要があるタイミングを示します。
Depthプロパティまたは DataMember プロパティ、または両方のプロパティを指定できます。 両方を指定することで、パフォーマンスが若干向上します。 ノードの深さは、バインドされるノード レベルを指定します。 たとえば、次の TreeNodeBinding 宣言では、データ ソースの Name フィールドと ID フィールドをそれぞれ、深さが 0 のすべてのノードの Text プロパティと Value プロパティにバインドします。
<asp:TreeNodeBinding Depth="0" TextField="Name" ValueField="ID">
データ メンバーは、基になるデータ ソース内のデータ項目の型を指定しますが、データ ソースに応じて異なる情報を表すことができます。 階層データ ソース ( System.Web.UI.IHierarchyData インターフェイスで表される) の各データ項目は、データ項目の種類を指定する IHierarchyData.Type プロパティを公開します。 たとえば、XML 要素のデータ メンバーは、要素の名前を指定します。 データ ソースに複数のデータ項目型が含まれている場合、データ メンバーは使用するデータ項目の種類を指定します。 次のTreeNodeBinding宣言では、階層内の場所に関係なく、<Book> コントロールのXmlDataSource要素をツリー内のすべてのノードにバインドします。
<asp:TreeNodeBinding DataMember="Book" TextField="Title" ValueField= "ISBN">
バインド条件が確立されたら、値にバインドできる TreeNode オブジェクトのプロパティをバインドできます。 データ項目のフィールドまたは静的な値にバインドできます。 静的な値にバインドすると、TreeNode オブジェクトが適用されるすべてのTreeNodeBinding オブジェクトが同じ値を共有します。
Note
ノード内で対応するプロパティを直接設定することで、 TreeNode オブジェクト内のバインドされたプロパティを選択的にオーバーライドできます。
次の表に、TreeNodeBinding オブジェクトのプロパティをデータ項目のフィールドにバインドできるTreeNode クラスのプロパティを示します。
| Property | Description |
|---|---|
| ImageUrlField | ImageUrl オブジェクトのTreeNode プロパティにバインドするフィールド。 |
| ImageToolTipField | ImageToolTip オブジェクトのTreeNode プロパティにバインドするフィールド。 |
| NavigateUrlField | NavigateUrl オブジェクトのTreeNode プロパティにバインドするフィールド。 |
| TextField | Text オブジェクトのTreeNode プロパティにバインドするフィールド。 |
| ToolTipField | ToolTip オブジェクトのTreeNode プロパティにバインドするフィールド。 |
| ValueField | Value オブジェクトのTreeNode プロパティにバインドするフィールド。 |
次の表に、TreeNodeBinding オブジェクトのプロパティを静的な値にバインドできるTreeNode クラスのプロパティを示します。
| Property | Description |
|---|---|
| ImageUrl | ImageUrl オブジェクトのTreeNode プロパティにバインドする静的な値。 |
| ImageToolTip | ImageToolTip オブジェクトのTreeNode プロパティにバインドする静的な値。 |
| NavigateUrl | NavigateUrl オブジェクトのTreeNode プロパティにバインドする静的な値。 |
| PopulateOnDemand | PopulateOnDemand オブジェクトのTreeNode プロパティにバインドする静的な値。 |
| SelectAction | SelectAction オブジェクトのTreeNode プロパティにバインドする静的な値。 |
| ShowCheckBox | ShowCheckBox オブジェクトのTreeNode プロパティにバインドする静的な値。 |
| Target | Target オブジェクトのTreeNode プロパティにバインドする静的な値。 |
| Text | Text オブジェクトのTreeNode プロパティにバインドする静的な値。 |
| ToolTip | ToolTip オブジェクトのTreeNode プロパティにバインドする静的な値。 |
| Value | Value オブジェクトのTreeNode プロパティにバインドする静的な値。 |
競合 TreeNodeBinding オブジェクトが定義されている場合、 TreeView コントロールは、ツリー ノードバインドを次の優先順位で適用します。
深度メンバーとデータ メンバーの両方を定義して一致する TreeNodeBinding オブジェクト。
データ メンバーのみを定義して一致する TreeNodeBinding オブジェクト。
深度のみを定義して一致する TreeNodeBinding オブジェクト。
深度もデータ メンバーも定義しない TreeNodeBinding オブジェクト。 (この種類のツリー ノード バインドは、ツリー内のすべてのノードに適用されます)。
データ ソースに一致するものがない TreeNodeBinding オブジェクト。 この場合、データ項目の
ToStringメソッドによって返される値は、Text オブジェクトが適用されるノードのValueプロパティとTreeNodeBindingプロパティにバインドされます。
TreeNodeBinding クラスでは、FormatString プロパティを設定することで、ノードに表示されるテキストの書式を設定することもできます。
コンストラクター
| 名前 | 説明 |
|---|---|
| TreeNodeBinding() |
TreeNodeBinding クラスの新しいインスタンスを初期化します。 |
プロパティ
| 名前 | 説明 |
|---|---|
| DataMember |
ツリー ノード バインドを適用するかどうかを判断するために、データ項目の Type プロパティと照合する値を取得または設定します。 |
| Depth |
TreeNodeBinding オブジェクトが適用されるノードの深さを取得または設定します。 |
| FormatString |
TreeNodeBinding オブジェクトが適用されるノードのテキストの表示形式を指定する文字列を取得または設定します。 |
| ImageToolTip |
TreeNodeBinding オブジェクトが適用されるノードの横に表示されるイメージのツールヒント テキストを取得または設定します。 |
| ImageToolTipField |
ImageToolTip オブジェクトが適用されるTreeNode オブジェクトのTreeNodeBinding プロパティにバインドするデータ ソースのフィールドの名前を取得または設定します。 |
| ImageUrl |
TreeNodeBinding オブジェクトが適用されるノードの横に表示されるイメージの URL を取得または設定します。 |
| ImageUrlField |
ImageUrl オブジェクトが適用されるTreeNode オブジェクトのTreeNodeBinding プロパティにバインドするデータ ソースのフィールドの名前を取得または設定します。 |
| NavigateUrl |
TreeNodeBinding オブジェクトが適用されているノードがクリックされたときにリンクする URL を取得または設定します。 |
| NavigateUrlField |
NavigateUrl オブジェクトが適用されるTreeNode オブジェクトのTreeNodeBinding プロパティにバインドするデータ ソースのフィールドの名前を取得または設定します。 |
| PopulateOnDemand |
TreeNodeBinding オブジェクトが適用されるノードが動的に設定されるかどうかを示す値を取得または設定します。 |
| SelectAction |
TreeNodeBinding オブジェクトが適用されているノードが選択されたときに発生するイベントを取得または設定します。 |
| ShowCheckBox |
TreeNodeBinding オブジェクトが適用されているノードの横にチェック ボックスを表示するかどうかを示す値を取得または設定します。 |
| Target |
TreeNodeBinding オブジェクトが適用されているノードに関連付けられている Web ページコンテンツを表示する対象ウィンドウまたはフレームを取得または設定します。 |
| TargetField |
Target オブジェクトが適用されるTreeNode オブジェクトのTreeNodeBinding プロパティにバインドするデータ ソースのフィールドの名前を取得または設定します。 |
| Text |
TreeNodeBinding オブジェクトが適用されるノードに表示されるテキストを取得または設定します。 |
| TextField |
Text オブジェクトが適用されるTreeNode オブジェクトのTreeNodeBinding プロパティにバインドするデータ ソースのフィールドの名前を取得または設定します。 |
| ToolTip |
TreeNodeBinding オブジェクトが適用されるノードのツールヒント テキストを取得または設定します。 |
| ToolTipField |
ToolTip オブジェクトが適用されるTreeNode オブジェクトのTreeNodeBinding プロパティにバインドするデータ ソースのフィールドの名前を取得または設定します。 |
| Value |
表示されていないが、ポストバック イベントの処理に使用されるデータなど、 TreeNodeBinding オブジェクトが適用されるノードに関する追加のデータを格納するために使用される表示値を取得または設定します。 |
| ValueField |
Value オブジェクトが適用されるTreeNode オブジェクトのTreeNodeBinding プロパティにバインドするデータ ソースのフィールドの名前を取得または設定します。 |
メソッド
| 名前 | 説明 |
|---|---|
| Equals(Object) |
指定したオブジェクトが現在のオブジェクトと等しいかどうかを判断します。 (継承元 Object) |
| GetHashCode() |
既定のハッシュ関数として機能します。 (継承元 Object) |
| GetType() |
現在のインスタンスの Type を取得します。 (継承元 Object) |
| MemberwiseClone() |
現在の Objectの簡易コピーを作成します。 (継承元 Object) |
| ToString() |
DataMember プロパティを返します。 |
明示的なインターフェイスの実装
| 名前 | 説明 |
|---|---|
| ICloneable.Clone() |
TreeNodeBinding オブジェクトのコピーを作成します。 |
| IDataSourceViewSchemaAccessor.DataSourceViewSchema |
このメンバーの説明については、 DataSourceViewSchemaを参照してください。 |
| IStateManager.IsTrackingViewState |
このメンバーの説明については、 IsTrackingViewStateを参照してください。 |
| IStateManager.LoadViewState(Object) |
ノードの以前に保存したビュー ステートを読み込みます。 |
| IStateManager.SaveViewState() |
ビュー ステートの変更をオブジェクトに保存します。 |
| IStateManager.TrackViewState() |
ビュー ステートの変更を追跡するように TreeNode オブジェクトに指示します。 |
適用対象
こちらもご覧ください
- TreeView
- TreeNode
- TreeNodeBindingCollection
- XmlDataSource
- DataBindings
- DataMember
- Depth
- FormatString
- ImageUrl
- ImageUrlField
- ImageToolTip
- ImageToolTipField
- NavigateUrl
- NavigateUrl
- NavigateUrlField
- PopulateOnDemand
- SelectAction
- ShowCheckBox
- Target
- Text
- Text
- TextField
- ToolTip
- ToolTip
- ToolTipField
- Value
- Value
- ValueField