i created multiple check boxes inside listbox but when i try to use these boxes in the c# code the code cant detect it but the check box outside the listbox is detected normally
this is the xaml list box
<code><ListBox x:Name="lb_mainElements" Grid.Row="2" Grid.ColumnSpan="2" Margin="10" Background="Transparent"
BorderThickness="0" SelectionChanged="ListBox_SelectionChanged_1">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem" x:Name="cTemplate" >
<Border Background="Transparent" Padding="5">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Content="Main Elements" FontWeight="Bold" Grid.Row="0" Grid.Column="0"/>
<!-- Checkboxes in a 3x3 grid -->
<CheckBox Content="Foundation" Grid.Row="1" Grid.Column="0" Margin="10" Name="cbFoundation" Unchecked="cbOtherCheckedChanged" Checked="cbOtherCheckedChanged"/>
<CheckBox Content="Columns" Grid.Row="1" Grid.Column="1" Margin="10" Name="cbColumns" Unchecked="cbOtherCheckedChanged" Checked="cbOtherCheckedChanged"/>
<CheckBox Content="Retaining Walls" Grid.Row="1" Grid.Column="2" Margin="10" Name="cbRwall" Unchecked="cbOtherCheckedChanged" Checked="cbOtherCheckedChanged"/>
<CheckBox Content="Shear Walls" Grid.Row="2" Grid.Column="0" Margin="10" Name="cbSwall" Unchecked="cbOtherCheckedChanged" Checked="cbOtherCheckedChanged"/>
<CheckBox Content="Beams" Grid.Row="2" Grid.Column="1" Margin="10" Name="cbBeams" Unchecked="cbOtherCheckedChanged" Checked="cbOtherCheckedChanged"/>
<CheckBox Content="Stairs" Grid.Row="2" Grid.Column="2" Margin="10" Name="cbStairs" Unchecked="cbOtherCheckedChanged" Checked="cbOtherCheckedChanged"/>
<CheckBox Content="Slab on Grade" Grid.Row="3" Grid.Column="0" Margin="10" Name="cbSog" Unchecked="cbOtherCheckedChanged" Checked="cbOtherCheckedChanged"/>
<CheckBox Content="Tanks" Grid.Row="3" Grid.Column="1" Margin="10" Name="cbTanks" Unchecked="cbOtherCheckedChanged" Checked="cbOtherCheckedChanged"/>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
<Label/>
</ListBox>
</code>
<code><ListBox x:Name="lb_mainElements" Grid.Row="2" Grid.ColumnSpan="2" Margin="10" Background="Transparent"
BorderThickness="0" SelectionChanged="ListBox_SelectionChanged_1">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem" x:Name="cTemplate" >
<Border Background="Transparent" Padding="5">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Content="Main Elements" FontWeight="Bold" Grid.Row="0" Grid.Column="0"/>
<!-- Checkboxes in a 3x3 grid -->
<CheckBox Content="Foundation" Grid.Row="1" Grid.Column="0" Margin="10" Name="cbFoundation" Unchecked="cbOtherCheckedChanged" Checked="cbOtherCheckedChanged"/>
<CheckBox Content="Columns" Grid.Row="1" Grid.Column="1" Margin="10" Name="cbColumns" Unchecked="cbOtherCheckedChanged" Checked="cbOtherCheckedChanged"/>
<CheckBox Content="Retaining Walls" Grid.Row="1" Grid.Column="2" Margin="10" Name="cbRwall" Unchecked="cbOtherCheckedChanged" Checked="cbOtherCheckedChanged"/>
<CheckBox Content="Shear Walls" Grid.Row="2" Grid.Column="0" Margin="10" Name="cbSwall" Unchecked="cbOtherCheckedChanged" Checked="cbOtherCheckedChanged"/>
<CheckBox Content="Beams" Grid.Row="2" Grid.Column="1" Margin="10" Name="cbBeams" Unchecked="cbOtherCheckedChanged" Checked="cbOtherCheckedChanged"/>
<CheckBox Content="Stairs" Grid.Row="2" Grid.Column="2" Margin="10" Name="cbStairs" Unchecked="cbOtherCheckedChanged" Checked="cbOtherCheckedChanged"/>
<CheckBox Content="Slab on Grade" Grid.Row="3" Grid.Column="0" Margin="10" Name="cbSog" Unchecked="cbOtherCheckedChanged" Checked="cbOtherCheckedChanged"/>
<CheckBox Content="Tanks" Grid.Row="3" Grid.Column="1" Margin="10" Name="cbTanks" Unchecked="cbOtherCheckedChanged" Checked="cbOtherCheckedChanged"/>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
<Label/>
</ListBox>
</code>
<ListBox x:Name="lb_mainElements" Grid.Row="2" Grid.ColumnSpan="2" Margin="10" Background="Transparent"
BorderThickness="0" SelectionChanged="ListBox_SelectionChanged_1">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem" x:Name="cTemplate" >
<Border Background="Transparent" Padding="5">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Content="Main Elements" FontWeight="Bold" Grid.Row="0" Grid.Column="0"/>
<!-- Checkboxes in a 3x3 grid -->
<CheckBox Content="Foundation" Grid.Row="1" Grid.Column="0" Margin="10" Name="cbFoundation" Unchecked="cbOtherCheckedChanged" Checked="cbOtherCheckedChanged"/>
<CheckBox Content="Columns" Grid.Row="1" Grid.Column="1" Margin="10" Name="cbColumns" Unchecked="cbOtherCheckedChanged" Checked="cbOtherCheckedChanged"/>
<CheckBox Content="Retaining Walls" Grid.Row="1" Grid.Column="2" Margin="10" Name="cbRwall" Unchecked="cbOtherCheckedChanged" Checked="cbOtherCheckedChanged"/>
<CheckBox Content="Shear Walls" Grid.Row="2" Grid.Column="0" Margin="10" Name="cbSwall" Unchecked="cbOtherCheckedChanged" Checked="cbOtherCheckedChanged"/>
<CheckBox Content="Beams" Grid.Row="2" Grid.Column="1" Margin="10" Name="cbBeams" Unchecked="cbOtherCheckedChanged" Checked="cbOtherCheckedChanged"/>
<CheckBox Content="Stairs" Grid.Row="2" Grid.Column="2" Margin="10" Name="cbStairs" Unchecked="cbOtherCheckedChanged" Checked="cbOtherCheckedChanged"/>
<CheckBox Content="Slab on Grade" Grid.Row="3" Grid.Column="0" Margin="10" Name="cbSog" Unchecked="cbOtherCheckedChanged" Checked="cbOtherCheckedChanged"/>
<CheckBox Content="Tanks" Grid.Row="3" Grid.Column="1" Margin="10" Name="cbTanks" Unchecked="cbOtherCheckedChanged" Checked="cbOtherCheckedChanged"/>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
<Label/>
</ListBox>
and this the c# code
<code>private void cbOtherCheckedChanged(object sender, RoutedEventArgs e)
{
bool newVal = (cbSelectAll.IsChecked == true);
cbFoundation.IsChecked = newVal;
cbColumns.IsChecked = newVal;
cbRwall.IsChecked = newVal;
cbSwall.IsChecked = newVal;
cbBeams.IsChecked = newVal;
cbStairs.IsChecked = newVal;
cbSog.IsChecked = newVal;
cbTanks.IsChecked = newVal;
}
</code>
<code>private void cbOtherCheckedChanged(object sender, RoutedEventArgs e)
{
bool newVal = (cbSelectAll.IsChecked == true);
cbFoundation.IsChecked = newVal;
cbColumns.IsChecked = newVal;
cbRwall.IsChecked = newVal;
cbSwall.IsChecked = newVal;
cbBeams.IsChecked = newVal;
cbStairs.IsChecked = newVal;
cbSog.IsChecked = newVal;
cbTanks.IsChecked = newVal;
}
</code>
private void cbOtherCheckedChanged(object sender, RoutedEventArgs e)
{
bool newVal = (cbSelectAll.IsChecked == true);
cbFoundation.IsChecked = newVal;
cbColumns.IsChecked = newVal;
cbRwall.IsChecked = newVal;
cbSwall.IsChecked = newVal;
cbBeams.IsChecked = newVal;
cbStairs.IsChecked = newVal;
cbSog.IsChecked = newVal;
cbTanks.IsChecked = newVal;
}
its my first time using WPF so i couldnt do much other than chatgpt but it didnt help
New contributor
Mostafa Sedik is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.