I have a checkbox inside a stackpanel inside a listbox. I need to set the visibility of that checkbox based on some logical operations, so I need to find that element in listbox.
I am using the following code to find the checkbox on my PageLoad, after the list`s dataSource has been set. But on this listboxItem i get a null value and a exception saying : reference is not a valid visual dependencyobject
private void searchListCheckBoxVisibility()
//private void SearchList_Loaded(object sender, RoutedEventArgs e)
{
try
{
ListBoxItem listItem = this.SearchList.ItemContainerGenerator.ContainerFromIndex(2) as ListBoxItem;
CheckBox targetCheckBox = FindFirstElementInVisualTree<CheckBox>(listItem);
}
}
The following is my xaml :
Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ListBox Margin="13,10,7,10" x:Name="SearchList" DoubleTap="SearchList_DoubleTap">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel x:Name="searchListPanel" Orientation="Horizontal" Width="400" ScrollViewer.HorizontalScrollBarVisibility="Auto">
<StackPanel Orientation="Vertical" Width="340">
<StackPanel Orientation="Vertical">
<TextBlock Text="Name : " FontWeight="Bold" Style="{StaticResource ResourceKey=MSFTGuidelines_TextBlock}"/>
<TextBlock Text="{Binding User}" TextWrapping="Wrap" Style="{StaticResource ResourceKey=MSFTGuidelines_TextBlock}"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Project : " FontWeight="Bold" Style="{StaticResource ResourceKey=MSFTGuidelines_TextBlock}"/>
<TextBlock Text="{Binding Project}" TextWrapping="Wrap" Style="{StaticResource ResourceKey=MSFTGuidelines_TextBlock}"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Status : " FontWeight="Bold" Style="{StaticResource ResourceKey=MSFTGuidelines_TextBlock}"/>
<TextBlock Text="{Binding On_OffBoarded}" TextWrapping="Wrap" Style="{StaticResource ResourceKey=MSFTGuidelines_TextBlock}"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Date : " FontWeight="Bold" Style="{StaticResource ResourceKey=MSFTGuidelines_TextBlock}"/>
<TextBlock Text="{Binding DT_On_OffBoarded}" TextWrapping="Wrap" Style="{StaticResource ResourceKey=MSFTGuidelines_TextBlock}"/>
</StackPanel>
<StackPanel>
<TextBlock Text=""/>
</StackPanel>
</StackPanel>
<CheckBox Width="60"
x:Name="user_Checkbox"
Content="" Tag="{Binding ID}"
Visibility="Collapsed"
Checked="user_Checkbox_Checked"
Unchecked="user_Checkbox_UnChecked"
Style="{StaticResource ResourceKey=MSFTGuidelinesCheckBox}"
IsChecked="False"
/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
I am stuck at this issue for a while, and it has been getting onto my nerves now, Please help. Thanks in advance.