Failed to read nested custom Listbox value in Datagrid WPF

I set my ListBoxItem for a Listbox that is nested in a datagrid. However, when I tried to loop through the datagrid to find the control for the list, but it failed when I tried to get the value of which switch is selected.

Can anyone advise you on any approach or some piece of solution that might help? Thank you very much.

<Page.Resources> <Style x:Key="RadioButtonItemStyle" TargetType="{x:Type ListBoxItem}"> <Setter Property="Margin" Value="0,0,5,0" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ListBoxItem}"> <Border BorderThickness="0" Background="Transparent"> <!-- Note: IsChecked is bound to IsSelected--> <RadioButton Focusable="False" IsHitTestVisible="False" IsChecked="{TemplateBinding IsSelected}"> <ContentPresenter /> </RadioButton> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> <ItemsPanelTemplate x:Key="HorizontalItemsPanel"> <VirtualizingStackPanel Orientation="Horizontal" /> </ItemsPanelTemplate> </Page.Resources> <Grid> <StackPanel> <StackPanel Orientation="Horizontal"> <Label Name="GroupQuestionHeader" FontSize="14" FontWeight="Bold" FontFamily="Times New Roman" /> <Label Name="PageCount" FontSize="10" FontFamily="Times New Roman"></Label> </StackPanel> <DataGrid AutoGenerateColumns="False" HorizontalAlignment="Left" Name="dataGrid1" VerticalAlignment="Top" CanUserReorderColumns="False" CanUserSortColumns="False" CanUserResizeColumns="False" CanUserAddRows="False"> <DataGrid.CellStyle> <Style TargetType="DataGridCell"> <Setter Property="Padding" Value="5" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type DataGridCell}"> <Border Padding="{TemplateBinding Padding}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True"> <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> </Border> </ControlTemplate> </Setter.Value> </Setter> <Style.Triggers> <Trigger Property="IsSelected" Value="true"> <Setter Property="Background" Value="Transparent" /> <Setter Property="Foreground" Value="Black" /> <Setter Property="BorderBrush" Value="{x:Null}" /> </Trigger> </Style.Triggers> </Style> </DataGrid.CellStyle> <DataGrid.Columns> <DataGridTemplateColumn Header="Question" Width="400"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock TextWrapping="Wrap" Text="{Binding QuestionContent, Mode=OneWay}" /> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> <DataGridTemplateColumn Header="We fully Comply | We partly Comply | We do not Comply" Width="*"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <ListBox BorderThickness="0" SelectedValue="{Binding MyDataListSelectedValue}" ItemContainerStyle="{StaticResource RadioButtonItemStyle}" ItemsPanel="{StaticResource HorizontalItemsPanel}" Name="OptionsRadioButtonGroup" HorizontalContentAlignment="Left" Cursor="Hand" HorizontalAlignment="Left"> <ListBoxItem Width="90"/> <ListBoxItem Width="90"/> <ListBoxItem/> </ListBox> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid> <Button Content="Next Page" Height="23" HorizontalAlignment="Right" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" /> </StackPanel> </Grid> 

Method used

 for (int i=0;i<dataGrid1.Items.Count;i++) { DataRowView datarowv = (DataRowView)dataGrid1.Items[i]; DataRow dr = datarowv.Row; string RBValue = dr.ItemArray[1].toString(); } 
+4
source share
1 answer

How about this one. I did not compile the code that I just wrote right from my head. Sorry if you get compilation errors. Just fix them.

 for (int i=0;i<dataGrid1.Items.Count;i++) { // get control which represents the data var control = dataGrid1.ItemContainerGenerator.ContainerFromItem(dataGrid1.Items[i]); DependencyObject obj = control // inside that control there is somewhere the ListBox so run down the visualtree till you find the damn ListBox for(;!(obj is ListBox); obj = VisualTreeHelper.GetChild(obj, 0)); ListBox listBox = obj as ListBox; if(listBox != null) { // get the selected values from ListBox var selectedItems = listBox.SelectedItems; foreach(var selectedItem in selecteditems) { Console.WriteLine("I am a selected item: " + selectedItem.ToString()); } } } 

If you don’t understand what this little code does, just ask.

+1
source

All Articles