Wpf finds all the checkboxes and TextBox in the control through VisualTreeHelper and binds them accordingly,
# Binding region CheckBox to TextBox
Dictionary <CheckBox, TextBox> CheckTextBoxDic = new Dictionary <CheckBox, TextBox> ();
// Find all the check boxes under the control and bind them to their corresponding text boxes
Private void FindAllCheckBox (DependencyObject reference)
{
For (int I = 0; I <VisualTreeHelper. GetChildrenCount (reference); I ++)
{
Var child = VisualTreeHelper. GetChild (reference, I );
If (child is CheckBox)
{
BindCheckBoxAndTextBox (child as CheckBox );
}
Else
{
FindAllCheckBox (child );
}
}
}
// Bind the text box corresponding to the check box and save it to the dictionary
Private void BindCheckBoxAndTextBox (CheckBox reference)
{
DependencyObject wrapPanel = VisualTreeHelper. GetParent (reference );
DependencyObject stackPanel = VisualTreeHelper. GetParent (wrapPanel );
CheckTextBoxDic. Add (reference, FindTextBox (stackPanel ));
}
// Locate the text box and return it
Private TextBox FindTextBox (DependencyObject reference)
{
For (int I = 0; I <VisualTreeHelper. GetChildrenCount (reference); I ++)
{
Var child = VisualTreeHelper. GetChild (reference, I );
If (child is TextBox)
{
Return child as TextBox;
}
Else
{
Return FindTextBox (child );
}
}
Return null;
}
# Endregion