WP8:不能在列表框中获取复选框 [英] Wp8:Not able to get checkBox in listbox
问题描述
我不能够找到列表框XAML复选框:
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>
我试图让cbk_state,这样我就可以设置它检查的财产,功能我用得到该复选框是
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);
}
}
的问题是,我得到ChildrenCount为零每次。
我已经通过几种方法了,但没有这样的use.Also试图
的这个
,但在这里我没有得到ItemContainerGenerator的列表框。
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.
我是新来WP8编程PLZ help.Thanks
I am new to wp8 programming plz help.Thanks
推荐答案
喜得到了解决的此处。有没有必要设置虚拟财产及其简单。
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);
}
}
就在孩子的DependencyObject有点变化= VisualTreeHelper.GetChild (LB,I);而不是VAR子
just a bit change at DependencyObject child = VisualTreeHelper.GetChild(lb, i); instead of var child
这篇关于WP8:不能在列表框中获取复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!