0

I have the following Code:

<ListBox Name="lstCheckBoxes" Grid.Row="4" Grid.RowSpan="5" Grid.Column="1" Grid.ColumnSpan="5">
    <CheckBox Name="CheckboxKlimaanlage" Content="Klimaanlage"/>
    <CheckBox Name="CheckboxSensor"   Content="Sensor"/>
    <CheckBox Name="CheckboxDigital"  Content="Digital"/>
    <CheckBox Name="CheckboxAnalog"   Content="Analog"/>
    <CheckBox Name="CheckboxAndere1"  Content="Anderes"/>
    <CheckBox Name="CheckboxAndere2"  Content="Anderes"/>
    <CheckBox Name="CheckboxAndere3"  Content="Anderes"/>
    <CheckBox Name="CheckboxAndere4"  Content="Anderes"/>
    <CheckBox Name="CheckboxAndere5"  Content="Anderes"/>
    <CheckBox Name="CheckboxAndere6"  Content="Anderes"/>
    <CheckBox Name="CheckboxAndere7"  Content="Anderes"/>
    <CheckBox Name="CheckboxAndere8"  Content="Anderes"/>
</ListBox>

and the issue is that I want to check every Checkbox inside the ListBox with a loop and save the content from the active boxes in something like a list.

I already saw some code like .. foreach (Control c in panel1.Controls).. but the .Controls won't work.

1 Answer 1

2

Controls property comes from WinForms project controls, I suppose

If you take a wpf Panel (Grid, StackPanel) - they have Children

but ListBox isn't a Panel, it is an ItemsControl and has Items

foreach (CheckBox c in lstCheckBoxes.Items)
{
    if (c.IsChecked == true)
    {
    }
}

it also has ItemsSource property, which can be set via binding. I recommend to read about MVVM in WPF, to use ItemsSource bound to a view model property.

Sign up to request clarification or add additional context in comments.

2 Comments

Great, this works. Thank you ! But How can i save the Checkbox Content as a String in something like a List ?
@Morta, assuming you have a list (var L = new List<string>();) declared somewhere before foreach loop, add text from Content property: L.Add(c.Content.ToString());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.