{"id":585,"date":"2010-10-06T14:30:27","date_gmt":"2010-10-06T14:30:27","guid":{"rendered":"http:\/\/dirk.schuermans.me\/?p=585"},"modified":"2016-01-07T10:48:07","modified_gmt":"2016-01-07T10:48:07","slug":"looping-through-child-controls-in-wpf-c","status":"publish","type":"post","link":"https:\/\/dirk.schuermans.me\/?p=585","title":{"rendered":"Looping through child controls in WPF (C#)"},"content":{"rendered":"<p>Hey everyone,<\/p>\n<p>As you all know, all I used to do was code all my stuff in Windows Forms. I&#8217;ve decided to switch over to WPF so I&#8217;m gently and at my own pace migrating from Windows Forms to WPF.<br \/>\nEveryone knows that when you migrate from one thing to another, you&#8217;ll always bump into the following problem:<\/p>\n<p>&#8220;I was able to do this before, how am I supposed to do this now? &#8221;<\/p>\n<p>That has already happend a few times to me while working in WPF.<\/p>\n<p>One of this issues I recently had, was that in Windows Forms it was easy to iterate over all the controls located within a form or container (GroupBox for example).<br \/>\nIn WPF, it obviously didn&#8217;t work that way &gt;.&lt;<\/p>\n<p><!--more--><\/p>\n<p>As an example, the following code in Windows Forms:<\/p>\n<pre name=\"code\" class=\"csharp\">\r\nforeach(Control c in GroupBox1.Controls)\r\n{\r\n\tif(c.GetType() == typeof(TextBox))\r\n\t{\r\n\t\tTextBox txt = (TextBox)c;\r\n\t\ttxt.Clear();\r\n\t}\r\n}\r\n<\/pre>\n<p>This would clear all textboxes located within GroupBox1.<\/p>\n<p>Now, when you try to do that in WPF, you&#8217;ll notice that the GroupBox doesn&#8217;t have a Controls property.<\/p>\n<p>So I had to put together a little helper that would get all the child controls for me and how deep they&#8217;d be nested:<\/p>\n<pre name=\"code\" class=\"csharp\">\r\nclass ChildControls\r\n    {\r\n        private List&lt;object&gt; lstChildren;\r\n\r\n        public List&lt;object&gt; GetChildren(Visual p_vParent, int p_nLevel)\r\n        {\r\n            if (p_vParent == null)\r\n            {\r\n                throw new ArgumentNullException(\"Element {0} is null!\", p_vParent.ToString());\r\n            }\r\n\r\n            this.lstChildren = new List&lt;object&gt;();\r\n\r\n            this.GetChildControls(p_vParent, p_nLevel);\r\n\r\n            return this.lstChildren;\r\n\r\n        }\r\n\r\n        private void GetChildControls(Visual p_vParent, int p_nLevel)\r\n        {\r\n            int nChildCount = VisualTreeHelper.GetChildrenCount(p_vParent);\r\n\r\n            for (int i = 0; i &lt;= nChildCount - 1; i++)\r\n            {\r\n                Visual v = (Visual)VisualTreeHelper.GetChild(p_vParent, i);\r\n                \r\n                lstChildren.Add((object)v);\r\n\r\n                if (VisualTreeHelper.GetChildrenCount(v) &gt; 0)\r\n                {\r\n                    GetChildControls(v, p_nLevel + 1);\r\n                }\r\n            }\r\n        }\r\n    }\r\n<\/pre>\n<p>Now with that class, I can simply do the following:<\/p>\n<pre name=\"code\" class=\"csharp\">\r\n            ChildControls ccChildren = new ChildControls();\r\n\r\n            foreach (object o in ccChildren.GetChildren(GroupBox1, 5))\r\n            {\r\n                if (o.GetType() == typeof(TextBox))\r\n                {\r\n                    TextBox txt = (TextBox)o;\r\n                    txt.Text = \"\";\r\n                }\r\n            }\r\n<\/pre>\n<p>Note that the 5 in &#8220;.GetChildren(GroupBox1, 5)&#8221; indicates how many levels deep I&#8217;d like to check for controls.<br \/>\nWhen you have a layout that contains deeply nested XAML code, you can easily increase this number, especially when requesting child controls for top level elements<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey everyone, As you all know, all I used to do was code all my stuff in Windows Forms. I&#8217;ve decided to switch over to WPF so I&#8217;m gently and at my own pace migrating from Windows Forms to WPF. Everyone knows that when you migrate from one thing to another, you&#8217;ll always bump into &hellip; <a href=\"https:\/\/dirk.schuermans.me\/?p=585\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Looping through child controls in WPF (C#)<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[38],"tags":[],"_links":{"self":[{"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/posts\/585"}],"collection":[{"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=585"}],"version-history":[{"count":9,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/posts\/585\/revisions"}],"predecessor-version":[{"id":771,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/posts\/585\/revisions\/771"}],"wp:attachment":[{"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=585"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=585"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=585"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}