【发布时间】:2012-01-20 09:23:33
【问题描述】:
我有一个 tabcontrol,里面有 9 个 tabitem。每个选项卡都有一系列文本框供用户输入数据。底部是一个清除按钮,与此方法挂钩:
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;
}
}
}
这工作得很好,但是我也有一个 MenuItem 允许用户清除所有选项卡。现在,清除按钮仅清除当前选定选项卡上的内容,而将其他所有内容单独保留。我对如何做到这一点的想法是用这个循环遍历 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);
}
}
}
}
【问题讨论】:
-
我认为这与使用 TabControl 的 WPF 行为有关。我已经读过,当一个选项卡消失时,前一个选项卡的可视化树会被“丢弃”。您可能希望将 Clear() 抽象为一个类。
标签: c# wpf button tabs automation