C# 循环使用longlistselector以获取itemtemplate中的复选框

C# 循环使用longlistselector以获取itemtemplate中的复选框,c#,foreach,windows-phone-8,itemtemplate,longlistselector,C#,Foreach,Windows Phone 8,Itemtemplate,Longlistselector,我有一个长长的列表选择器: <phone:LongListSelector Background="Transparent" x:Name="DocSummaries" ItemTemplate="{StaticResource DataTemplate_Header}" ItemsSource="{Binding DocumentHeaders}" Margin="33,0,-5,0"/> 如果有人能在这里帮助我,我将不胜感激 使用Visual Studio 2012,c#Wi

我有一个长长的列表选择器:

<phone:LongListSelector Background="Transparent"  x:Name="DocSummaries" ItemTemplate="{StaticResource DataTemplate_Header}" ItemsSource="{Binding DocumentHeaders}" Margin="33,0,-5,0"/>
如果有人能在这里帮助我,我将不胜感激


使用Visual Studio 2012,c#Windows phone 8应用程序

虽然您当然可以像您尝试的那样在代码隐藏中这样做,但我建议您寻找MVVM实现。 代码将更加干净,并且易于维护

其思想是创建一个包含
ChildViewModel
列表的
MainViewModel
。 每个
ChildViewModel
都包含
ItemTemplate
中所需的所有信息,包括一个
布尔属性,该属性将绑定
复选框
。 您可以将
LongListSelector.ItemsSource
绑定到
MainViewModel
ChildViewModels
属性

通过这种方式,您可以轻松截获选中/取消选中
复选框的时刻(通过布尔属性设置器),并将信息传递给其他ChildViewModels,以便它们进行更改

根据您的评论进行编辑,指出代码必须存在于代码背后:

您可以创建一个函数,在
VisualTreeHelper
类的帮助下递归搜索
checbox

LongListSelector FoundList = (Mylist as LongListSelector);
SearchElement(FoundList);
以下是函数:

        private void SearchElement(DependencyObject targeted_control)
        {
            var count = VisualTreeHelper.GetChildrenCount(targeted_control);
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    var child = VisualTreeHelper.GetChild(targeted_control, i);
                    if (child is CheckBox) // Only search for ChecBoxes
                    {
                        CheckBox targeted_element = (CheckBox)child;
                        // check/uncheck
                    }
                    else
                    {
                        SearchElement(child);
                    }
                }
            }
            else
            {
                return;
            }
        }
private void SearchElement(DependencyObject-targeted\u控件)
{
var count=VisualTreeHelper.GetChildrenCount(目标控制);
如果(计数>0)
{
for(int i=0;i
Hi Olivier,感谢您的洞察力,问题是,我们公司在一个核心库上运行,其中包含所有共享的内容,创建这些模型将改变核心库,我们的大多数屏幕都是动态构建的。。。在代码隐藏中,这是我唯一需要在模板中获得特定控件的地方。非常非常酷。感谢您的理解和耐心,从未听说过这个visualtreehelper类,但它确实工作得很好,没有迟缓或滞后。谢谢你,伙计PIt很遗憾,当我们有一个长名单的多选者时,这不起作用。。。它引发StackOverflow异常:(
LongListSelector FoundList = (Mylist as LongListSelector);
SearchElement(FoundList);
        private void SearchElement(DependencyObject targeted_control)
        {
            var count = VisualTreeHelper.GetChildrenCount(targeted_control);
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    var child = VisualTreeHelper.GetChild(targeted_control, i);
                    if (child is CheckBox) // Only search for ChecBoxes
                    {
                        CheckBox targeted_element = (CheckBox)child;
                        // check/uncheck
                    }
                    else
                    {
                        SearchElement(child);
                    }
                }
            }
            else
            {
                return;
            }
        }