Bagikan melalui


TreeNode.ShowCheckBox Properti

Definisi

Mendapatkan atau mengatur nilai yang menunjukkan apakah kotak centang ditampilkan di samping simpul.

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)

Nilai Properti

true untuk menampilkan kotak centang; jika tidak, false.

Contoh

Contoh kode berikut menunjukkan cara menggunakan ShowCheckBox properti untuk secara terprogram memperlihatkan dan menyembunyikan kotak centang untuk simpul dalam TreeView kontrol. Agar contoh ini berfungsi dengan benar, Anda harus menyalin data XML sampel di bawah ini ke file bernama 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>

Kode berikut adalah contoh data XML untuk contoh sebelumnya.

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

Keterangan

TreeView Kontrol memungkinkan Anda menampilkan kotak centang di samping gambar simpul. ShowCheckBox Gunakan properti untuk memperlihatkan atau menyembunyikan kotak centang untuk simpul saat ini.

ShowCheckBox Meskipun properti dapat digunakan untuk menampilkan kotak centang, lebih umum untuk menggunakan TreeView.ShowCheckBoxes properti TreeView kontrol. Properti TreeView.ShowCheckBoxes , bagaimanapun, memengaruhi setiap jenis node yang ditentukan oleh properti; oleh karena itu, TreeNode.ShowCheckBox properti sering digunakan untuk mengambil alih pengaturan tersebut untuk simpul individual.

TreeView.ShowCheckBoxes Karena properti adalah properti tri-state, cuplikan kode C# berikut menyebabkan kesalahan kompilasi:

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

Meskipun VB.Net secara implisit melemparkan Boolean nilai ke NullableBoolean, C# tidak. Oleh karena itu, ini adalah praktik terbaik untuk secara eksplisit memeriksa status properti. Misalnya, contoh kode berikut di Visual Basic dan C# secara eksplisit menguji nilai Expanded properti.

Contoh kode Visual Basic berikut secara eksplisit menguji nilai Expanded properti. Contoh ini menguji apakah Expanded properti diatur ke True; oleh karena itu Nothing dan False jatuh melalui If pernyataan.

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

Contoh kode C# ini secara eksplisit menguji nilai Expanded properti. Contoh ini menguji apakah Expanded properti diatur ke True; oleh karena itu Null dan False jatuh melalui If pernyataan.

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

Nilai properti ini disimpan dalam status tampilan.

Berlaku untuk

Lihat juga