我有一个tabcontrol中有9个tabitems的tabcontrol。每个选项卡都有一系列文本框供用户输入数据。在底部是一个清除按钮,迷上了这种方法:遍历TabControl中的TabItems
public void ClearTextBoxes()
{
ChildControls ccChildren = new ChildControls();
foreach (object o in ccChildren.GetChildren(rvraDockPanel, 2))
{
if (o.GetType() == typeof(WatermarkTextBox) || o.GetType() == typeof(DateBox) ||
o.GetType() == typeof(DigitBox) || o.GetType() == typeof(PhoneBox))
{
WatermarkTextBox water = (WatermarkTextBox)o;
water.Text = "";
}
else if (o.GetType() == typeof(ComboBox))
{
ComboBox combo = (ComboBox)o;
combo.SelectedIndex = -1;
}
else if (o.GetType() == typeof(CheckBox))
{
CheckBox check = (CheckBox)o;
check.IsChecked = false;
}
}
}
这工作完全正常,但我也有一个菜单项,允许用户ClearAll标签。现在,清除按钮只会清除当前选中的选项卡上的内容,并保留其他所有内容。我如何做到这一点的想法是通过这个循环中的TabItems迭代:
for (int i = 0; i < 10; i++)
{
tabSelection.SelectedIndex = i;
clearButton_Click(null, null);
}
它会翻阅所有选项卡,但不会明确的东西。我试过使用自动化代替,结果相同。它似乎并不清楚任何事情。
ChildControls类:
class ChildControls
{
private List<object> listChildren;
public List<object> GetChildren(Visual p_vParent, int p_nLevel)
{
if (p_vParent == null)
{
throw new ArgumentNullException("Element {0} is null!", p_vParent.ToString());
}
this.listChildren = new List<object>();
this.GetChildControls(p_vParent, p_nLevel);
return this.listChildren;
}
private void GetChildControls(Visual p_vParent, int p_nLevel)
{
int nChildCount = VisualTreeHelper.GetChildrenCount(p_vParent);
for (int i = 0; i <= nChildCount - 1; i++)
{
Visual v = (Visual)VisualTreeHelper.GetChild(p_vParent, i);
listChildren.Add((object)v);
if (VisualTreeHelper.GetChildrenCount(v) > 0)
{
GetChildControls(v, p_nLevel + 1);
}
}
}
}
我认为这与WPF行为与TabControl有关。我已经读过,当一个标签离开视图时,前一个标签的可视化树会被“丢弃”。您可能想要将Clear()抽象为一个类。 – 2011-12-14 16:04:12