#region CheckBox與TextBox綁定 Dictionary<CheckBox, TextBox> CheckTextBoxDic = new Dictionary<CheckBox, TextBox>(); //找到控制項下所有勾選框並與其相對應的文本框綁定 private void ...
#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