I would say your code snippet does not even compile, because it is a mix between WPF and Windows Form.
WPF:
private void btnClear_Click(object sender, RoutedEventArgs e)
WinForm:
foreach (Control control in this.Controls)
. WPF "controls" do not have a property Controls...
Following does clear TextBox in WPF:
private void Button_Click(object sender, RoutedEventArgs e)
{
ClearTextBoxes(this);
}
private void ClearTextBoxes(DependencyObject parentDependencyObject)
{
int visualChildrenCount = VisualTreeHelper.GetChildrenCount(parentDependencyObject);
for (int i = 0; i < visualChildrenCount; i++)
{
DependencyObject dependencyObject = VisualTreeHelper.GetChild(parentDependencyObject, i);
TextBox textBox = dependencyObject as TextBox;
if (textBox != null)
{
textBox.Clear();
}
else
{
ClearTextBoxes(dependencyObject);
}
}
}
But I would suggest you to go for a more general solution like described here:
c# - Find all controls in WPF Window by type - Stack Overflow[
^]
I hope it helps.