Click here to Skip to main content
15,894,106 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I'm creating a pretty basic user control that I can best describe as similar to a border with some custom dependency properties. I would like to place what ever the user lodes as content in the center of my user control. Hear is a stripped down mock up of what I'm trying to do.

XML
<usercontrol1>
   <grid>
     <border>
        <"this is where i would like content to load="">
     <border>
   <grid>
<usercontrol1>


So when used in XAML the below would result in a text block surrounded by a border.

XML
<window>
   <usercontrol1>
      <textblock text="Surround me with a border" margin="5">
   <usercontrol1>
<window>


This is just a mock up and will not compile. I don't have an IDE where I'm at. If no one has answered before I go to work tomorrow I'll update with valid code. Also remember that my actual user control has a lot more to it. I stripped this down to simplify the question so please don't provide answers that suggest not using a user control unless its because I'll need to use a custom control.

What I have tried:

I tried using a content presenter but no matter what I've done user loaded content replaced everything in the user control.
Posted
Updated 1-Dec-16 23:51pm
v3
Comments
Philippe Mori 1-Dec-16 22:55pm    
Well, the following search might give you some hints : bing: wpf decorator control. I don't know enough about WPF to help you more.
ryanba29 1-Dec-16 23:32pm    
Thank you Phillpe. I did find some useful information on using a control template with a user control that looks like it may do what I need here https://www.codeproject.com/articles/82464/how-to-embed-arbitrary-content-in-a-wpf-control. I'll give it a try.
ryanba29 2-Dec-16 6:15am    
Philippe Mori, the decorator was the wrong solution in this case but it did get me looking in the right place. If you want to use the solution I posted and post something similar I'll accept your answer unless someone comes up with a better one. I would hate to asked the question and then accept my own answer.
Philippe Mori 2-Dec-16 10:13am    
Well, it is still better to provide an answer from other comments that to have no answer at all. I am not very experimented with WPF. The only thing, I have done is guessing a relatively good starting point search query.

1 solution

Using Philippe Moris advise I can across that content presenter must be applied with a ControlTemplate or your just setting the content property twice and that was why my entire control was replaced by user supplied content. The following code worked for me, but after you build the project the XAML designer throws a 'MyControl' ControlTemplate TargetType does not match templated type 'UserControl' error. This is a bug and removing the bin and obj folders from the project removes the error and the designer can be used again until its is rebuilt.

XML
<UserControl x:Class="WpfApplication20.MyControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WpfApplication20"
             mc:Ignorable="d">
    <UserControl.Template>
        <ControlTemplate TargetType="{x:Type local:MyControl}">
            <Border Background="Black" BorderBrush="Black">
                <ContentPresenter></ContentPresenter>
            </Border>
        </ControlTemplate>
    </UserControl.Template>
</UserControl>
 
Share this answer
 
v7
Comments
Philippe Mori 2-Dec-16 10:15am    
For completeness, it might be good that you would provide links that were useful for you and a bit more explanation (or code) on how this is used.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900