1

am having a set of checkbox in a stackpanel, i want to get the selected checkbox in my code..

how i can get those selected checkbox in a stackpanel

1
  • Not sure what your scenario is, but if these checkboxes represent data points (e.g. which purchase orders are approved) rather than being controls with a specific behavioural purpose, it's easier and more idiomatic in WPF to use a databound ItemsControl or ListBox, with the CheckBox as part of its DataTemplate. Commented Mar 28, 2010 at 7:26

1 Answer 1

1

You can query the children of the stackpanel.

IEnumerable<CheckBox> selectedBoxes =
    from checkbox in this.stackPanel1.Children.OfType<CheckBox>()
    where checkbox.IsChecked.Value
    select checkbox;

foreach (CheckBox box in selectedBoxes)
{
    // do something 
}

Same query in lambda form

IEnumerable<CheckBox> selectedBoxes =
    this.stackPanel1.Children.OfType<CheckBox>()
    .Where(cb => cb.IsChecked.Value);
Sign up to request clarification or add additional context in comments.

2 Comments

You can make this easier with the OfType operator: from cb in stackPanel1.Children.OfType<CheckBox>() where cb.IsChecked.Value select cb. Saves the test in the where clause and the repeated casts.
hai itowlson, i can t understand wat you saying. can u tell me in some more detailed...

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.