1

我有这个长列表选择器:

<phone:LongListSelector Background="Transparent"  x:Name="DocSummaries" ItemTemplate="{StaticResource DataTemplate_Header}" ItemsSource="{Binding DocumentHeaders}" Margin="33,0,-5,0"/>

并且该列表使用此 itemtemplate :

<Viewbox x:Name="viewboxHeader" Tag="0">
                <StackPanel Orientation="Vertical" x:Name="listStackpanel" Height="330" Width="410" Margin="0,10,0,8">
                    <Grid x:Name="HeaderGrid"  Margin="0,0,0,0" Height="325" Width="410" Background="#FF4A4A4A" >
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="150"/>
                        </Grid.ColumnDefinitions>
                        <!--<TextBlock x:Name="txtduration" TextWrapping="Wrap"  RenderTransformOrigin="0.5,0.5" FontSize="{StaticResource PhoneFontSizeNormal}" Text="{Binding Transaction.Duration}" Foreground="{Binding Transaction.Urgency, Converter={StaticResource converttocolor}}" TextAlignment="Left"  Width="60" Height="28" Canvas.Left="-26" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="-18,0,0,25" >

                        </TextBlock>
                        <TextBlock x:Name="txtdays" TextWrapping="Wrap" Text="days" Foreground="{Binding Transaction.Urgency, Converter={StaticResource converttocolor}}" RenderTransformOrigin="0.5,0.5" FontSize="{StaticResource PhoneFontSizeSmall}"  TextAlignment="Left"  Width="45" Height="28" Canvas.Left="-26" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="3,0,0,10" >

                        </TextBlock>-->
                        <CheckBox x:Name="chkSelect" Grid.Row="0" Grid.Column="0" Grid.RowSpan="6" VerticalAlignment="Center" Width="Auto" Checked="chkSelect_Checked" Unchecked="chkSelect_Unchecked"/>

                        <StackPanel Grid.Row="0" Grid.Column="2"  Margin="15,0,0,5" x:Name="Durationpanel" Background="#FF4A4A4A" HorizontalAlignment="Right" Orientation="Horizontal">
                            <TextBlock TextWrapping="Wrap" Margin="2" FontSize="22" x:Name="txtDuration" Text="{Binding Duration}" Foreground="{Binding Urgency, Converter={StaticResource converttocolor}}" HorizontalAlignment="Right"></TextBlock>
                            <TextBlock TextWrapping="Wrap" Margin="2,4,2,2" FontSize="22" Foreground="{Binding Urgency, Converter={StaticResource converttocolor}}" Text="days" HorizontalAlignment="Right"></TextBlock>
                        </StackPanel>

                        <TextBlock Grid.Row="1" Grid.Column="1" Tap="btnViewDetail_Tap" Margin="0" Grid.ColumnSpan="2" TextWrapping="Wrap" Text="{Binding DocDesc}" FontSize="30" Foreground="#FFDFDFDF" >                               
                        </TextBlock>

                        <TextBlock TextWrapping="Wrap" Margin="0" Grid.Row="2" Grid.ColumnSpan="2" Grid.Column="1" Text="{Binding Created}" FontSize="16" Foreground="#FFDFDFDF">
                        </TextBlock>

                        <TextBlock TextWrapping="Wrap" Grid.Row="3" Grid.ColumnSpan="2" Grid.Column="1" Margin="0" Text="{Binding Actioned}" Foreground="#FFDFDFDF" FontSize="16">
                        </TextBlock>

                        <TextBlock Grid.Row="4" Grid.Column="1" Margin="0,5,0,0" Foreground="#FFDFDFDF" FontSize="16" Text="{Binding Info1Label}"></TextBlock>
                        <TextBox Grid.Row="4" HorizontalAlignment="Stretch" Grid.Column="2" Height="55" FontSize="16" Margin="0,0,0,-15" Text="{Binding Info1Value}"></TextBox>

                        <TextBlock Grid.Row="5" Grid.Column="1" Foreground="#FFDFDFDF" FontSize="16" Text="{Binding Info2Label}" ></TextBlock>
                        <TextBox Grid.Row="5" HorizontalAlignment="Stretch" Grid.Column="2" Height="55" FontSize="16" Margin="0,0,0,-15" Text="{Binding Info2Value}"></TextBox>

                        <TextBlock Grid.Row="6" Grid.Column="1" Foreground="#FFDFDFDF" FontSize="16" Text="{Binding Info3Label}"></TextBlock>
                        <TextBox Grid.Row="6" HorizontalAlignment="Stretch" Grid.Column="2" Height="55" FontSize="16" Margin="0,0,0,-15" Text="{Binding Info3Value}"></TextBox>
                    </Grid>
                </StackPanel>
            </Viewbox>

现在我的问题是,我想在 longlistselector 中的 itemtemplate 中获取复选框,这样如果用户单击一个复选框,其他复选框就会被取消选中。

在下面的代码中,我得到了 longlistselector,但现在我不知道如何从 itemtemplate 中获取复选框。

 foreach (var thisthing in LayoutRoot.Children)
        {
            if (thisthing.GetType() == typeof(Grid))
            {
                Grid myGrid = (thisthing as Grid);

                foreach (var Mylist in myGrid.Children)
                {
                    if (Mylist.GetType() == typeof(LongListSelector))
                    {
                        LongListSelector FoundList = (Mylist as LongListSelector);                           

                    }
                }



            }
        }

如果有人可以在这里帮助我,我将不胜感激。

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

4

1 回答 1

3

虽然您当然可以像您尝试的那样在代码隐藏中执行此操作,但我建议您寻找 MVVM 实现。代码将更清洁且可维护。

这个想法是您将创建一个MainViewModel包含ChildViewModel. 每个都ChildViewModel包含您在 中需要的所有信息ItemTemplate,包括将绑定的boolean属性。CheckBox您将 绑定LongListSelector.ItemsSource到 的ChildViewModels属性MainViewModel

这样,您可以轻松地拦截 aCheckBox被选中/取消选中的时刻(通过布尔属性 Setter)并将信息传递给其他 ChildViewModel 以供它们更改。

编辑,根据您的评论表明代码必须存在于代码隐藏中:

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

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;
            }
        }
于 2013-07-17T09:34:03.677 回答