To achieve this you can place the controls inside a container (in this case a StackPanel).
This way you can easily access the Group of Controls you need.
Note(s):
- In the pseudo code representation bellow; I'm separating the functionality in order to simplify understanding so it can be easily adapted to suit your needs.
Example Controls
This example uses 2 Buttons:
- A Reset Button: To Reset TextBoxes Text
- A Toggle Button: To Toggle CheckBoxes Checked State.
The CheckBoxes and Textboxes are each declared inside a StackPanel.
This allows accessing a group of Controls that are inside a Container and therefore to perform the action(s) required.
- A StackPanel containing CheckBoxes (Bellow Renamed to: "sp_CheckBoxes").
- A StackPanel containing TextBoxes (Bellow Renamed to: "sp_TextBoxes").
Example Events
- Both Buttons have the Same Event assigned ("Button_Click").
- The Event itself will check wich Button was Pressed and Act Accordingly.
XAML
<!-- Buttons -->
<Button x:Name="btn_ResetText" Click="Button_Click" Content = "Reset" />
<Button x:Name="btn_TogglecheckBoxes" Click="Button_Click" Content = "Toggle" />
<!-- Stack Panel Containing CheckBoxes -->
<StackPanel x:Name="sp_CheckBoxes">
<CheckBox x:Name="myCheckBox1" Content="ContentText1"/>
<CheckBox x:Name="myCheckBox2" Content="ContentText2"/>
<CheckBox x:Name="myCheckBox3" Content="ContentText3"/>
<CheckBox x:Name="myCheckBox4" Content="ContentText4"/>
<CheckBox x:Name="myCheckBox5" Content="ContentText5"/>
</StackPanel>
<!-- Stack Panel Containing TextBoxes -->
<StackPanel x:Name="sp_TextBoxes">
<TextBox x:Name="myTextBox1" Text="Text1"/>
<TextBox x:Name="myTextBox2" Text="Text2"/>
<TextBox x:Name="myTextBox3" Text="Text3"/>
<TextBox x:Name="myTextBox4" Text="Text4"/>
<TextBox x:Name="myTextBox5" Text="Text5"/>
</StackPanel>
C#
// The Button Click Event Assigned for Both Buttons
private void Button_Click(object sender, RoutedEventArgs e)
{
// When one of the Button is Pressed:
// Filter wich Button was Pressed
Button btn = (Button)sender;
// Act Accordingly
switch (btn.Name)
{
case "btn_ToggleCheckBoxes": ToggleCheckBoxes(); break;
case "btn_ResetText": ResetText(); break;
}
}
// The Method to Toggle CheckBoxes
private void ToggleTextBoxes()
{
foreach (CheckBox chkb in sp_CheckBoxes.Children)
{
if ((bool)chkb.IsChecked) { chkb.IsChecked = false; }
else { chkb.IsChecked = true; }
}
}
// The Method to Clear TextBoxes
private void ResetText()
{
foreach (TextBox txt in sp_TextBoxes.Children)
{
txt.Clear();
}
}