1

I am trying to achieve the following thing but I am not sure if this is possible in WPF and if it possible what I should look for. I want to have one base UserControl which will have common parts and a child based part somewhere in the control which will be different for each control which use the base UserControl. I made a simple picture to illustrate this: enter image description here

So every UserControl which inherit the Base UserControl should define the controls inside the child content. Is this possible?

mm8
  • 163,881
  • 10
  • 57
  • 88
Milen Grigorov
  • 332
  • 3
  • 13
  • Instead of inheriting from UserControl you may create a Style that replaces the UserControl's Template like e.g. this: https://stackoverflow.com/a/10427420/1136211 – Clemens Feb 14 '19 at 13:28
  • look for a content presenter it will allow you to display xaml elements inside your control A button for examle does exactly that you can put custom xaml inside of a button while the button stays what he is – Denis Schaf Feb 14 '19 at 13:28
  • Is there a particular reason that this isn't just one usercontrol with a contentcontrol in it? Or a contentcontrol with a template that adds all the stuff around your content for that matter. What behavior do the usercontrols encapsulate? – Andy Feb 14 '19 at 14:30

1 Answers1

1

You could define a common ControlTemplate somewhere, for example in your App.xaml file:

<Application.Resources>
    <ResourceDictionary>
        <ControlTemplate x:Key="CommonTemplate" TargetType="UserControl">
            <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" 
                    Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <TextBlock Text="Label" />
                    <TextBox Grid.Column="1" />

                    <TextBlock Grid.Row="1" Text="Label" />
                    <TextBox Grid.Row="1" Grid.Column="1" />

                    <ContentPresenter Grid.Row="2" Grid.ColumnSpan="2" 
                                        HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                        SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
                                        VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>

                    <TextBlock Grid.Row="3" Text="Label" />
                    <TextBox Grid.Row="3" Grid.Column="1" />
                </Grid>
            </Border>
        </ControlTemplate>
    </ResourceDictionary>
</Application.Resources>

You could then apply this template to your UserControls. The Content of the UserControl will end up where the ContentPresenter is located in the template, e.g.:

<UserControl Template="{StaticResource CommonTemplate}">
    <TextBlock>child content....</TextBlock>
</UserControl>
mm8
  • 163,881
  • 10
  • 57
  • 88
  • Thank you, this looks like what I am looking for. One additional question, can I have 2 different ContentPresenters? And if I can how can I define where to go each part of the usercontrol? – Milen Grigorov Feb 14 '19 at 15:20
  • No. The `UserControl` has only one `Content` property. – mm8 Feb 14 '19 at 15:21