[英]Wp8:Not able to get checkBox in listbox
I am not able to find checkbox in listbox xaml: 我在列表框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 我正在尝试获取cbk_state,以便我可以设置其checked属性。我用来获取复选框的功能是
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. 问题是,我得到ChildrenCount零每次。 I have gone through several methods but no as such of use.Also tried this but here i am not getting ItemContainerGenerator for listBox.
我已经经历了几种方法,但没有使用过。也尝试过这种方法,但是在这里我没有得到ListBox的ItemContainerGenerator。
I am new to wp8 programming plz help.Thanks 我是新来的WP8编程PLZ help.Thanks
Are you asking about getting the Checked
property of the Checkbox
? 您是否在询问获取
Checkbox
的Checked
属性?
Is this the one you were looking for?. 是这个你要找的人? Sample code to find the
Children
control within a Parent
using VisualTreeHelper
: 使用
VisualTreeHelper
在Parent
找到Children
控件的示例代码:
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); DependencyObject child = VisualTreeHelper.GetChild(lb,i); instead of var child
而不是VAR孩子
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.