当前位置: 移动技术网 > IT编程>开发语言>.net > wpf通过VisualTreeHelper找到控件内所有CheckBox和TextBox并做相应绑定

wpf通过VisualTreeHelper找到控件内所有CheckBox和TextBox并做相应绑定

2018年03月03日  | 移动技术网IT编程  | 我要评论

黑鸟恋人,秦时明月第六部,FL7733A

#region CheckBox与TextBox绑定

Dictionary<CheckBox, TextBox> CheckTextBoxDic = new Dictionary<CheckBox, TextBox>();
//找到控件下所有勾选框并与其相对应的文本框绑定
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);
}

}
}

//绑定勾选框与其对应的文本框,存入字典
private void BindCheckBoxAndTextBox(CheckBox reference)
{
DependencyObject wrapPanel = VisualTreeHelper.GetParent(reference);
DependencyObject stackPanel = VisualTreeHelper.GetParent(wrapPanel);
CheckTextBoxDic.Add(reference, FindTextBox(stackPanel));
}

//找到相应文本框并将其返回
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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网