Question

I have This Long list selector :

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

and that list uses this 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>

Now my problem is, i want to get the checkbox in that itemtemplate that is in the longlistselector, so that if the user clicks on a checkbox, the other checkboxes get unchecked.

in the code below i get the longlistselector, but now i have no idea how to get the checkbox form the 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);                           

                    }
                }



            }
        }

if anyone can help me here i would appreciate it.

Using Visual Studio 2012, c# Windows phone 8 Application

Was it helpful?

Solution

While you can certainly do that in the code-behind like you tried, I advice you to look for an MVVM implementation. The code will be much cleaner an maintainable.

The idea is that you would create a MainViewModel that contains a List of ChildViewModel. Each ChildViewModel contain all the information you need in the ItemTemplate, including a boolean property that the CheckBox will be bound on. You bind the LongListSelector.ItemsSource to the ChildViewModels property of the MainViewModel.

That way, you can easily intercept the moment that a CheckBox is checked/unchecked (via the boolean property Setter) and pass the info to the other ChildViewModels for them to change.

EDIT, based on your comment indicating that the code has to live in the code-behind:

You could create a function that will search for ChecBoxes recursively with the help of the VisualTreeHelper class:

LongListSelector FoundList = (Mylist as LongListSelector);
SearchElement(FoundList);

Here is the function:

        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;
            }
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top