Wpf finds all the checkboxes and TextBox in the control through VisualTreeHelper and binds them accordingly,

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.