My goal is to send a wpf control and enable or disable all controls in its container.
I found this code in stack overflow and i can loop alla elements but i dont now how to convert it to a System.Windows.Control so that i can set the isEnabled flag:
public static void EnableAllControls(Visual control, int level,bool enable)
{
const int indent = 4;
int ChildNumber = VisualTreeHelper.GetChildrenCount(control);
for (int i = 0; i <= ChildNumber - 1; i++)
{
Visual v = (Visual)VisualTreeHelper.GetChild(control, i);
if (VisualTreeHelper.GetChildrenCount(v) > 0)
{
EnableAllControls(v, level + 1,enable);
}
//var ctr = v.GetType as System.Windows.Controls.Control; -> this gives runtime error.
//ctr.IsEnabled = enable;
}
}
I quess it should be possible - does anyone now how?