views:

148

answers:

1

This is a bit of a noob question but I've just realised that if i create a UserControl and choose to name some of its child elements a la -

<UserControl x:Class="UserControls.uControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300">
<Grid x:Name="maingrid">

</Grid>

then i can reference the named elements in the code-behind of the consuming XAML. So for the example above i could write

uControl.mainGrid = new Grid();

I'm curious as to why this is the case and more importantly, how i can safely encapsulate these controls.

As usual, ANY help would be really appreciated.

+1  A: 

What you want is the x:FieldModifier attribute:

<UserControl x:Class="UserControls.uControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="300" Height="300">
    <Grid x:Name="maingrid" x:FieldModifier="private">
    </Grid>
</UserControl>

The default is internal, for some reason. You can read more about it here.

Robert Macnee
lol. That's the worst design decision I've seen yet! Thanks man, big help.
Stimul8d