Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何填充和访问复选框列表?VB.NET WPF_Wpf_Vb.net_Checkbox - Fatal编程技术网

如何填充和访问复选框列表?VB.NET WPF

如何填充和访问复选框列表?VB.NET WPF,wpf,vb.net,checkbox,Wpf,Vb.net,Checkbox,(这是对上一个问题的补充。) 我有一个树视图,其中有一个父节点和两个子节点。每个节点都包含一个存储在TreeViewItem中的复选框。当用户选中父节点时,我希望两个子节点复选框设置为IsChecked=true;当用户取消选中父节点时,我希望两个子节点复选框设置为IsChecked=false 我有一个for循环,其中子节点复选框存储在一个列表中。父节点的复选框选中/取消选中事件应在子节点复选框列表中迭代,但我在chkbox_AllChecked函数(父节点的复选框选中/取消选中事件)中的子复

(这是对上一个问题的补充。)

我有一个树视图,其中有一个父节点和两个子节点。每个节点都包含一个存储在TreeViewItem中的复选框。当用户选中父节点时,我希望两个子节点复选框设置为IsChecked=true;当用户取消选中父节点时,我希望两个子节点复选框设置为IsChecked=false

我有一个for循环,其中子节点复选框存储在一个列表中。父节点的复选框选中/取消选中事件应在子节点复选框列表中迭代,但我在chkbox_AllChecked函数(父节点的复选框选中/取消选中事件)中的子复选框列表有问题。由于某种原因,列表是空的。我认为问题在于如何在For循环中填充列表。谁能解释我做错了什么

这是我的密码:

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 chk = 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
谢谢你的帮助

好吧,我想出来了

1) 我不希望这一行:Dim ChildCheckBox(sites.Length)作为复选框 因为这是一个数组而不是一个列表

2) 删除第一行后,我需要更改:childCheckbox(I)=childCheckbox 添加(childCheckbox)


就这样!完成。其余部分可以使用。

您是否应该使用
CheckedListBox
而不是
列表(CheckeckBox)