Avalon Control Collection - UIElementCollection

One of the first things I noticed when trying to iterate through the controls of a Longhorn form looking for a control was the absence of the Controls class.  In its place is the UIElementCollection, a member of the MSAvalon.Windows.Controls namespace for the PDC build. This is an array which you can use to gain access to the child controls of an element from the codebehind.  For example, if you had this scenario in the XAML document:

<Canvas ID=”FunControls” Width=”100%” Height=”100%”>

<Button ID=“B0“>I am button 1</Button>
<Button ID=“B1“>I am button 2</Button>

</Canvas>

You may get to the child elements of that Canvas with the following in the codebehind:

UIElementCollection ec = FunControls.Children;
//now I would like to find all of the buttons
for (int i=0; i < ec.Count; i++)
{

//note this namespace will change in future builds...
if (ec[i].GetType.ToString() == “MSAvalon.Windows.Controls.Button” )
{

MSAvalon.Windows.Controls.Button b = (MSAvalon.Windows.Controls.Button)ec[i];
//do something with the button here :)

}

}

Very easy, but the syntax is slightly different from past WinForms programming

No Comments