TreeNodeBinding クラスとは? わかりやすく解説

TreeNodeBinding クラス

メモ : このクラスは、.NET Framework version 2.0新しく追加されたものです。

データ項目と、それが TreeView コントロール内でバインドしているノードとの関係を定義します

名前空間: System.Web.UI.WebControls
アセンブリ: System.Web (system.web.dll 内)
構文構文

Public NotInheritable Class
 TreeNodeBinding
    Implements IStateManager, ICloneable, IDataSourceViewSchemaAccessor
Dim instance As TreeNodeBinding
public sealed class TreeNodeBinding : IStateManager,
 ICloneable, IDataSourceViewSchemaAccessor
public ref class TreeNodeBinding sealed : IStateManager,
 ICloneable, IDataSourceViewSchemaAccessor
public final class TreeNodeBinding implements
 IStateManager, ICloneable, 
    IDataSourceViewSchemaAccessor
public final class TreeNodeBinding implements
 IStateManager, ICloneable, 
    IDataSourceViewSchemaAccessor
解説解説

複数属性を持つ XML 要素どのように、各データ項目複数フィールド含まれているデータ ソースTreeView コントロールバインドされた場合ノードにはデータ項目ToString メソッドから返される値が既定表示されます。XML 要素場合ノードには要素名が表示されます。これはメニュー ツリーの基になる構造体を示す以外はあまり用途はありません。ノードプロパティは、ツリー ノードバインディング指定して特定のフィールドバインドできますTreeNodeBinding オブジェクトは、各データ項目とそのバインド先のノードとの関係を定義します

TreeView コントロールは、その TreeNodeBinding オブジェクトを DataBindings プロパティ格納しデータ ソースバインディング適用してツリー階層構造データ ソース階層構造との間に一対一リレーションシップ作成しますデータ ソース内のデータ項目について、TreeView コントロールは、対応する TreeNode オブジェクト作成するために、データ項目TreeNodeBinding オブジェクトとの照合試みます

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 宣言は、階層構造の場所に無関係に、XmlDataSource コントロール<Book> 要素ツリー内のすべてのノードバインドます。

<asp:TreeNodeBinding DataMember="Book" TextField="Title" ValueField= "ISBN">

バインディング基準確立されたら、バインド可能な TreeNode オブジェクトプロパティを値にバインドできますデータ項目フィールドまたは静的な値にバインドできますTreeNodeBinding オブジェクト静的な値がバインドされた場合、そのオブジェクト適用先となるすべての TreeNode オブジェクトは、同じ値を共有します

メモメモ

TreeNode オブジェクト内のバインドされたプロパティは、ノード内で対応するプロパティ直接設定することにより、選択してオーバーライドできます

TreeNode オブジェクトプロパティデータ項目フィールドバインドできる、TreeNodeBinding クラスプロパティの一覧を次の表に示します

TreeNode オブジェクトプロパティ静的な値にバインドできる、TreeNodeBinding クラスプロパティの一覧を次の表に示します

競合する TreeNodeBinding オブジェクト定義されている場合TreeView コントロール次の優先順位ツリー ノードバインディング適用します。

  1. 深さデータ メンバ両方定義し照合する TreeNodeBinding オブジェクト

  2. データ メンバだけを定義し照合する TreeNodeBinding オブジェクト

  3. 深さだけを定義し照合する TreeNodeBinding オブジェクト

  4. 深さデータ メンバ定義しない TreeNodeBinding オブジェクト。この種のツリー ノード バインディングは、ツリー内のすべてのノード適用されます。

  5. データ ソース内に一致するものがない TreeNodeBinding オブジェクト。この場合データ項目ToString メソッドによって返される値は、TreeNodeBinding オブジェクト適用されるノードText プロパティValue プロパティバインドされます

TreeNodeBinding クラスでは、FormatString プロパティ設定することにより、ノード表示されるテキスト書式設定することもできます

使用例使用例

ツリー ノード バインディング宣言の例を次の表に示します

バインディングの例

説明

<asp:TreeNodeBinding TextField="Title" ValueField= "ID"/>

ツリー内のすべてのノードText プロパティValue プロパティを、データ ソースTitle フィールドID フィールドそれぞれバインドます。DataMember プロパティDepth プロパティ設定されていないため、すべてのノードでこのツリー ノード バインディング宣言使用されます。

<asp:TreeNodeBinding DataMember= "Book" TextField= "Title" ValueField= "ID"/>

ツリー内のすべてのノードText プロパティValue プロパティを、データ ソースBook データ項目Title フィールドID フィールドそれぞれバインドます。

<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 プロパティを、データ ソースBook データ項目Title フィールドID フィールドそれぞれバインドます。また、ノードImageUrl プロパティ静的な値にバインドます。

このセクションには、3 つのコード例含まれています。TreeNodeBinding オブジェクト宣言によって使用しノードデータ項目との関係を定義する方法最初コード例示しますTreeNodeBinding オブジェクトプログラム使用しノードデータ項目との関係を定義する方法2 番目のコード例示します。1 番目と 2 番目のコード例対応したサンプル XML データ3 番目のコード例示します

TreeNodeBinding オブジェクト宣言によって使用しノードデータ項目との関係を定義する方法次のコード例示します。この例を正常に動作させるには、このコード例の後に示すサンプル XML データを、Book.xml という名前のファイルコピーする必要があります

<%@ Page Language="VB" %>

<html>
  <body>
    <form 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="C#" %>

<html>
  <body>
    <form 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="VB" %>

<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>
  <body>
    <form 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="C#" %>

<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>
  <body>
    <form 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>

前の 2 つコード例対応したサンプル 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>
継承階層継承階層
System.Object
  System.Web.UI.WebControls.TreeNodeBinding
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
TreeNodeBinding メンバ
System.Web.UI.WebControls 名前空間
TreeView
TreeNode クラス
TreeNodeBindingCollection
XmlDataSource
DataBindings
DataMember
Depth
FormatString
TreeNodeBinding.ImageUrl
ImageUrlField
ImageToolTip
ImageToolTipField
TreeNode.NavigateUrl プロパティ
TreeNodeBinding.NavigateUrl
NavigateUrlField
PopulateOnDemand
SelectAction
ShowCheckBox
Target
TreeNode.Text プロパティ
TreeNodeBinding.Text
TextField
TreeNode.ToolTip プロパティ
TreeNodeBinding.ToolTip
ToolTipField
TreeNode.Value プロパティ
TreeNodeBinding.Value
ValueField



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「TreeNodeBinding クラス」の関連用語

TreeNodeBinding クラスのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



TreeNodeBinding クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS