开发者

How to populate and access a list of checkboxes? VB.NET WPF

开发者 https://www.devze.com 2023-01-26 13:40 出处:网络
(This a follow-up to a previous question. VB.NET WPF NullReference Exception) I have a TreeView with a parent node and two children node. Each of the nodes contain a checkbox stored in a TreeViewItem

(This a follow-up to a previous question. VB.NET WPF NullReference Exception)

I have a TreeView with a parent node and two children node. Each of the nodes contain a checkbox stored in a TreeViewItem. I want the two children node checkboxes to be set to IsChecked=true when the user checks the parent node and I want the two children node checkboxes to be IsChecked=false when the user unchecks the parent node.

I have a for loop in which the child node checkboxes are stored in a list. The parent node's checkbox check/uncheck event should iterate through the child node checkbox list but I am having a problem with the childCheckboxes list in the chkbox_AllChecked function (the parent node's checkbox check/uncheck event). For some reason the list is empty. I think the problem is how I try to populate the list while inside a For loop. Can anyone explain what I am doing wrong?

Here's My Code:

Public Class Question

Dim childCheckbox As CheckBox
Dim childCheckboxes As New List(Of CheckBox)

Public Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded

    Dim parentCheckbox = New CheckBox
    Dim parentNode = New TreeViewItem

    parentCheckbox.Uid = "All Sites"

    AddHandler parentCheckbox.Checked, AddressOf chkbox_AllChecked
    AddHandler parentCheckbox.Unchecked, AddressOf chkbox_AllUnchecked

    parentCheckbox.Content = "All Sites"
    parentNode.Header = parentCheckbox

   Dim i As Integer = 0
   Dim childCheckboxes(sites.Length) As CheckBox 

    For Each osite In sites

                Dim childNode = New TreeViewItem
                Dim childCheckbox = New CheckBox

                AddHandler childCheckbox.Checked, AddressOf chkbox_Checked
                AddHandler childCheckbox.Unchecked, AddressOf chkbox_Unchecked

                childCheckbox.Uid = osite.SiteName.ToString

                childCheckbox.Content = osite.SiteName.ToString
                childNode.Header = childCheckbox
                parentNode.Items.Add(childNode)

                'Add all childCheckbox to an array for use by parentChildbox methods to check/uncheck all

                childCheckboxes(i) = childCheckbox

                i += 1

            Next
            TreeView1.Items.Add(parentNode)


    End Sub

Private Sub chkbox_AllChecked(ByVal sender As Object, ByVal e As RoutedEventArgs)
    Dim c开发者_运维技巧hk = DirectCast(sender, CheckBox)

            'MessageBox.Show(chk.Uid.ToString)


            'This part doesn't work. 
            For Each c In childCheckboxes
                c.IsChecked = True
            Next

    End Sub

Private Sub chkbox_Checked(ByVal sender As Object, ByVal e As RoutedEventArgs)
  Dim chk = DirectCast(sender, CheckBox)

            'MessageBox.Show("Check!")
            MessageBox.Show(chk.Uid.ToString)

End Sub

Private Sub chkbox_Unchecked(ByVal sender As Object, ByVal e As RoutedEventArgs)
   Dim chk = DirectCast(sender, CheckBox)

          'MessageBox.Show("Uncheck!")
           MessageBox.Show(chk.Uid.ToString)

End Sub

End Class

Thanks for the help!


Ok I figured this out.

1) I didn't want this line: Dim childCheckboxes(sites.Length) As CheckBox because that's an array not a list.

2) Once that first line is deleted I needed to change: childCheckboxes(i) = childCheckbox to this: childCheckboxes.Add(childCheckbox)

That's it! Done. The rest works.

0

精彩评论

暂无评论...
验证码 换一张
取 消