UserControl Content property

I have looked around quite a bit now, but still havn't found a way to instruct the UserControl to insert any data put in the Content property at my desired location. Here is a snippet:

<UserControl x:Class="MyNameSpace.MyControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Name="myControl"
        VerticalContentAlignment="Stretch"
    HorizontalContentAlignment="Stretch"
        >
    <StackPanel>
        <Button Content="{Binding ElementName=myControl,Path=Title}" />
        <Button Content="Button 2" />
       <!-- I want any content put here -->
    </StackPanel>
</UserControl>


So doing the above I would like to be able to do (in my window):

<nn:MyControl Title="Hello">Some internal content</nn:MyControl>

Running this as it is now replaces my stackpanel and buttons with just the text-string.

So what I want is some kind of tag or something that shows that I want the content at a specified location, and not to override my custom control contents.


Answer this question

UserControl Content property

  • Mario Esposito

    You just made my day!

    Info for others reading this post:
    To bind to my custom DependencyProperty named "Title" I still had to do
    {Binding ElementName=userControl,Path=Title}
    where userControl is the x:Name of the UserControl. Why Because doing a templatebinding to Title doesn't work since the template binding is done on the UserControl class, not the custom UserControl1 class. Guess there are workarounds, but this works.

    Oh, and there is an error in the XML schema. I get this warning on the DataTemplate element

    The element 'UserControl.ContentTemplate' in namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation' has invalid child element 'DataTemplate' in namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation'. List of possible elements expected: 'sgMarkupExtension, sgResourceKey, ComponentResourceKey, sgTemplateKey, DataTemplateKey, sgBindingBase, Binding, MultiBinding, PriorityBinding, RelativeSource, DynamicResource, ColorConvertedBitmapExtension, StaticResource, TemplateBinding, ThemeDictionary, sgDataTemplate, HierarchicalDataTemplate' in namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation'.

    It compiles and runs perfectly though.


  • Timski72

    Since a UserControl is basically just a ContentControl, the markup for your UserControl is its content.

    Thus, when you "instantiate" your UserControl with content ("Some internal content" in your example), you end up replacing the content defined in your UserControl markup (e.g., <StackPanel><Button ...> etc.) with the content from the instantiation markup ("Some internal content").

    Instead, what you probably want is this:

    <UserControl x:Class="UserControl1"
    xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
    xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
    VerticalContentAlignment="Stretch"
    HorizontalContentAlignment="Stretch">
    <
    UserControl.ContentTemplate>
    <
    DataTemplate>
    <
    StackPanel Orientation="Horizontal">
    <
    Button Content="Button 1"/>
    <
    ContentPresenter Content="{TemplateBinding Content}"/>
    <
    Button Content="Button 2"/>
    </
    StackPanel>
    </
    DataTemplate>
    </
    UserControl.ContentTemplate>
    </
    UserControl>

    Now, the content from your instantiation will appear between the two Buttons.

    I'm certainly no WPF expert at this point, but I think this is the right way to do it.


  • UserControl Content property