Wp8:Not able to get checkBox in listbox

2019-05-18 10:11发布

问题:

I am not able to find checkbox in listbox xaml:

<ListBox x:Name="my_list" Grid.Row="0">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" >
                        <CheckBox x:Name="cbx_state"  Tag="{Binding}"/>
                        <TextBlock x:Name="txt_string" Text="{Binding}" VerticalAlignment="Center" FontSize="34" />
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
 </ListBox>

I am trying to get cbk_state so that i can set its checked property.The function i used to get the checkbox is

private void GetItemsRecursive(DependencyObject lb)
  {
      var childrenCount = VisualTreeHelper.GetChildrenCount(lb);

      for (int i = 0; i < childrenCount; i++)
      {
          var child = VisualTreeHelper.GetChild(lb, i);


          if (child is ListBoxItem)
          {
              MessageBox.Show(child.GetType().ToString());
              return;
          }

          GetItemsRecursive(child);
      }
  }

The problem is that i am getting ChildrenCount as zero everytime. I have gone through several methods but no as such of use.Also tried this but here i am not getting ItemContainerGenerator for listBox.

I am new to wp8 programming plz help.Thanks

回答1:

Are you asking about getting the Checked property of the Checkbox?

Is this the one you were looking for?. Sample code to find the Children control within a Parent using VisualTreeHelper:

 private ChildControl FindVisualChild<ChildControl>(DependencyObject DependencyObj)
    where ChildControl : DependencyObject
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(DependencyObj); i++)
        {
            DependencyObject Child = VisualTreeHelper.GetChild(DependencyObj, i);

            if (Child != null && Child is ChildControl)
            {
                return (ChildControl)Child;
            }
            else
            {
                ChildControl ChildOfChild = FindVisualChild<ChildControl>(Child);

                if (ChildOfChild != null)
                {
                    return ChildOfChild;
                }
            }
        }
        return null;
    }


回答2:

Hi got the solution here. there is no need to set virtualization property its simple.

private void GetItemsRecursive(DependencyObject lb)
{
    var childrenCount = VisualTreeHelper.GetChildrenCount(lb);

    for (int i = 0; i < childrenCount; i++)
    {
        DependencyObject child = VisualTreeHelper.GetChild(lb, i);

        if (child is CheckBox) // specific/child control 
        {
            CheckBox targeted_element = (CheckBox)child;

            targeted_element.IsChecked = true;

            if (targeted_element.IsChecked == true)
            {

                return;
            }
        }

        GetItemsRecursive(child);
    }
}

just a bit change at DependencyObject child = VisualTreeHelper.GetChild(lb, i); instead of var child