简体   繁体   中英

Wp8:Not able to get checkBox in listbox

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM