Question

I have a Silverlight UserControl which uses the ContentPropertyAttribute to exposes the Children property of one of it's child panels. This allows me to add child controls to the panel on my page:

<local:MyUserControl>
    <TextBox Name="tbTest" />
</local:MyUserControl>

This works, apart from the 'tbTest' field of the page being present, but not initialised. On closer inspection, the InitializeComponent method does try to locate the TextBox (with FindName), but fails to do so (returning null).

After some investigation, I've found that namescopes are the problem - the UserControl has it's own namescope, thus it's children can't be located with the page's FindName but can with the UserControl's FindName method.

How can I alter my UserControl so that the child controls are locatable by the InitializeComponent method? The standard Panels (StackPanel, Grid, etc.) don't seem to have any problem doing so, so there must be a solution?

Thanks

Was it helpful?

Solution

It may be difficult to do at this point but the best course of action would probably be to derive your control from ItemsControl instead of UserControl. Then you wouldn't have the problem with name scopes.

I suppose as a workaround you could do a dive into the control with VisualTreeHelper to manually set the tbTest field.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top