TreeNode.ShowCheckBox 屬性

定義

取得或設定值,表示節點旁是否顯示核取方塊。

public:
 property Nullable<bool> ShowCheckBox { Nullable<bool> get(); void set(Nullable<bool> value); };
public bool? ShowCheckBox { get; set; }
member this.ShowCheckBox : Nullable<bool> with get, set
Public Property ShowCheckBox As Nullable(Of Boolean)

屬性值

Nullable<Boolean>

true 表示顯示核取方塊,否則為 false

範例

下列程式碼範例示範如何使用 ShowCheckBox 屬性,以程式設計方式顯示和隱藏 控制項中節點的 TreeView 核取方塊。 若要讓此範例正常運作,您必須將下面的範例 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 Data_Bound(Object sender, TreeNodeEventArgs e)
  {

    // Determine the depth of a node as it is bound to data.
    // If the depth is 1, show a check box.
    if(e.Node.Depth == 1)
    {

      e.Node.ShowCheckBox = true;

    }
    else
    {

      e.Node.ShowCheckBox = false;

    }

  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>TreeNode ShowCheckBox Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>TreeNode ShowCheckBox Example</h3>
    
      <asp:TreeView id="BookTreeView" 
         DataSourceID="BookXmlDataSource"
         OnTreeNodeDataBound="Data_Bound"
         ShowCheckBoxes="None"
         ExpandDepth="2"  
         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">
<script runat="server">

  Sub Data_Bound(ByVal sender As Object, ByVal e As TreeNodeEventArgs)

    ' Determine the depth of a node as it is bound to data.
    ' If the depth is 1, show a check box.
    If e.Node.Depth = 1 Then

      e.Node.ShowCheckBox = True

    Else

      e.Node.ShowCheckBox = False

    End If

  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>TreeNode ShowCheckBox Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>TreeNode ShowCheckBox Example</h3>
    
      <asp:TreeView id="BookTreeView" 
         DataSourceID="BookXmlDataSource"
         OnTreeNodeDataBound="Data_Bound"
         ShowCheckBoxes="None"
         ExpandDepth="2"  
         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>

下列程式碼是上一個範例的 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>  

備註

控制項 TreeView 可讓您顯示節點影像旁的核取方塊。 ShowCheckBox使用 屬性來顯示或隱藏目前節點的核取方塊。

ShowCheckBox雖然 屬性可以用來顯示覆選框,但使用 TreeView.ShowCheckBoxes 控制項的 TreeView 屬性比較常見。 不過,屬性 TreeView.ShowCheckBoxes 會影響屬性所指定的每個節點類型;因此, TreeNode.ShowCheckBox 屬性通常用來覆寫個別節點的該設定。

TreeView.ShowCheckBoxes由於 屬性是三狀態屬性,因此下列 C# 程式碼片段會造成編譯錯誤:

protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)  
{  
if (TreeView1.Nodes[0].Expanded)  
{  
// some work here   
}  
}  

雖然 VB.Net 會隱含地將 BooleanNullableBoolean 轉換成 ,但 C# 則不會。 因此,最好明確地檢查屬性的狀態。 例如,Visual Basic 和 C# 中的下列程式碼範例會明確測試 屬性的值 Expanded

下列Visual Basic程式碼範例會明確測試 屬性的值 Expanded 。 這個範例會測試 屬性是否設定為 True ,因此 NothingFalse 落在 語句中 IfExpanded

If TreeView1.Nodes(0).Expanded = True Then 'some work hereEnd IF  

這個 C# 程式碼範例會明確測試 屬性的值 Expanded 。 這個範例會測試 屬性是否設定為 True ,因此 NullFalse 落在 語句中 IfExpanded

if( TreeView1.Nodes[0].Expanded == true ) { //some work here}  

這個屬性的值會儲存在檢視狀態中。

適用於

另請參閱