UserControl and FindName Controls

I have a Silverlight UserControl that uses a ContentPropertyAttribute to display the Children property of one of the child panels. This allows me to add child controls to a panel on my page:

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

This works, except for the 'tbTest' field of the page present but not initialized. Upon closer inspection, the InitializeComponent method tries to find a TextBox (with FindName), but cannot do this (returns null).

After some investigation, I found that name problems are a problem, UserControl has its own name, so children cannot be located with the FindName page, but they can using the UserControl FindName method.

How can I change my UserControl so that the child controls are localized by the InitializeComponent method? Standard panels (StackPanel, Grid, etc.) seem to have no problems with this, so there should be a solution?

thank

+3
source share
1 answer

This may be difficult to do at this point, but the best course of action is probably to gain control over the ItemsControl instead of UserControl. Then you will not have problems with namespaces.

I suggest that as a workaround, you can dive into the control using VisualTreeHelper to manually set the tbTest field.

+1
source

Source: https://habr.com/ru/post/1782948/


All Articles