I'm creating a custom control by deriving from ContentView
. My goal is to have a user consume the class as you would expect to consume any control with a single piece of content:
<local:NewControl>
<Image source="..." />
</local:NewControl>
However, I would like the control to contain a grid and then have the content placed in the grid. Conceptually (i.e. ignoring the fact that ContentPresenter
cannot be used in this way), I'd like NewControl.xaml
to look like this:
<ContentView ...>
<Grid x:Name="HiddenGrid">
<ContentPresenter />
</Grid>
</ContentView>
Doing what I detailed above results in HiddenGrid
being replaced by the Image
, rather than the image being inserted in the visual tree as a child of the ContentPresenter
.
At the moment, I'm achieving my goal by deriving from Grid
instead of ContentView
, but I'd like to hide this implementation detail so consumers cannot interfere with the Grid
and have a control to which they can assign only a single piece of content.
This seems like it should be straightforward (and widely applicable to all sorts of scenarios where content needs to be embellished in some way), but I can't see a way of doing it. Am I missing something obvious, is it not possible, or is it just tricky to do and I need to read up on something (like control templates)?