XAML 是否可以独立控制嵌套在treeView中的复选框?

wixjitnu  于 2023-04-27  发布在  其他
关注(0)|答案(1)|浏览(85)

我有一个输出树视图的代码,每个树视图都有一个复选框。是否可以重命名每个复选框以反映它所显示的内容。

<Grid>
   <TreeView ItemsSource="{Binding Source={StaticResource xmlData},XPath=//@Name}">
      <TreeView.ItemTemplate>
         <DataTemplate>
            <CheckBox Name="Check1">
               <TreeViewItem Header="{Binding XPath.}">
                  <StackPanel>
                     <ListView Grid.Row="2" Grid.Column="2" ItemsSource="{Binding XPath=..//@Foodname}">
                        <ListView.View>
                           <GridView>
                              <GridViewColumn Header="Selection">
                                 <GridViewColumn.CellTemplate>
                                    <DataTemplate>
                                       <CheckBox Name="Check"/>
                                    </DataTemplate>
                                 </GridViewColumn.CellTemplate>
                              </GridViewColumn>
                              <GridViewColumn>
                              </GridViewColumn>
                           </GridView>
                        </ListView.View>
                     </ListView>
                  </StackPanel>
               </TreeViewItem>
            </CheckBox>
         </DataTemplate>
      </TreeView.ItemTemplate>
   </TreeView>
</Grid>
<Foods>
    <UnhealthyFoods Name ="Candy"/>
        <Food Foodname = "MMs" >
        </Food>
        <Food Foodname = "Skittles" >
        </Food>
        <Food Foodname = "Starburst" >
        </Food>
    </UnhealthyFoods>
    <HealthyFoods>
        <FoodGroup1 Name ="Vegetables"/>
        <Food Foodname = "Spinach" >
            </Food>
            <Food Foodname = "Brocolli" >
            </Food>
            <Food Foodname = "Greens" >
            </Food>
    </FoodGroup1>
    </HealthyFoods>
    <HealthyFoods>
        <FoodGroup2 Name ="Fruits"/>
        <Food Foodname = "Apples" >
            </Food>
            <Food Foodname = "Pears" >
            </Food>
            <Food Foodname = "Cherries" >
            </Food>
    </FoodGroup2>
    </HealthyFoods>
</Foods>

这是xml数据,我不关心它是如何写的,我无法对它进行更改。
我尝试使用,但错误说“你不能在空值表达式上调用方法。

[System.Windows.RoutedEventHandler]$check_checked={param($sender,$e) write-host "testing"}

Check.AddHandler([System.Windows.Controls.Checkbox])::CheckedEvent,$check_checked)

所以为了好玩,我尝试在树视图中没有的不同位置创建另一个复选框,上面的代码确实工作了。

zrfyljdw

zrfyljdw1#

您可以使用VisualTreeHelper Class来查找TreeView中的所有复选框:

Public Function FindCheckboxes(container As DependencyObject) As List(Of CheckBox)
    Dim checkboxes As New List(Of CheckBox)

    For counter As Integer = 0 To VisualTreeHelper.GetChildrenCount(container) - 1

        Dim child As DependencyObject = VisualTreeHelper.GetChild(container, counter)

        If child.GetType = GetType(CheckBox) Then
            checkboxes.Add(child)
        End If

        For Each childCheckbox As CheckBox In FindCheckboxes(child)
            checkboxes.Add(childCheckbox)
        Next

    Next

    Return checkboxes

End Function

一旦构建了树,您就可以调用此函数,并将事件处理程序添加到它返回的List中的每个复选框中。
下面是一个示例,说明如何使用它在books ListView中显示复选框列表:

Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
    Dim list As String = ""
    For Each checkbox As CheckBox In FindCheckboxes(books)
        list &= checkbox.Name & vbCrLf
    Next
    MsgBox(list)
End Sub

在您的例子中,您将把TreeView传递给它,并检查它返回的CheckBox控件的IsChecked属性。
如果你想改变他们的名字,你可以使用相同的函数:

Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
    Dim counter As Integer = 0
    Dim checkboxes As List(Of CheckBox) = FindCheckboxes(foods)

    For Each checkbox As CheckBox In checkboxes

        If checkbox.Content IsNot Nothing Then
            If checkbox.Content.GetType = GetType(TreeViewItem) Then
                Dim item As TreeViewItem = CType(checkbox.Content, TreeViewItem)
                Dim attribute As XmlAttribute = CType(item.Header, XmlAttribute)
                checkbox.Name = attribute.Value
            End If
        Else
            Dim attribute As XmlAttribute = CType(checkbox.DataContext, XmlAttribute)
            checkbox.Name = attribute.Value
        End If

        Debug.WriteLine(checkbox.Name)

    Next
End Sub

相关问题