Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a UserControl example that allows for UIElement content:</p> <pre class="lang-cs prettyprint-override"><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Windows.Markup; namespace TestClean.UserControls { [ContentProperty("Child")] public partial class CustomBorder : UserControl { public static readonly DependencyProperty ChildProperty = DependencyProperty.Register("Child", typeof(UIElement), typeof(CustomBorder), new UIPropertyMetadata(null)); public UIElement Child { get { return (UIElement)GetValue(ChildProperty); } set { SetValue(ChildProperty, value); } } public CustomBorder() { InitializeComponent(); } } } </code></pre> <pre class="lang-xml prettyprint-override"><code>&lt;UserControl x:Class="TestClean.UserControls.CustomBorder" 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" Name="control"&gt; &lt;Border BorderThickness="1" CornerRadius="10" BorderBrush="Red" Background="Pink" Padding="5"&gt; &lt;Border BorderThickness="1" CornerRadius="8" BorderBrush="Blue" Background="LightBlue" Padding="5"&gt; &lt;ContentPresenter Content="{Binding ElementName=control, Path=Child}" /&gt; &lt;/Border&gt; &lt;/Border&gt; &lt;/UserControl&gt; </code></pre> <p>Usage example:</p> <pre><code>&lt;uc:CustomBorder&gt; &lt;TextBlock Text="Lorem Ipsum"/&gt; &lt;/uc:CustomBorder&gt; </code></pre> <p>Looks like:</p> <p><img src="https://i.stack.imgur.com/FTQch.png" alt="Sreenshot"></p> <p>Just modify the borders at will.</p> <hr> <p><strong>Edit:</strong> To actually answer the question and somewhat reiterate what i said in a comment to Tim's answer, UserControls are appropriate for this since they directly encapsulate the visual representation and allow for composition while not requiring much custom logic (in theory they can be very complex though and include a lot of logic). They are more lightweight and immediate than CustomControls.</p> <p>In WPF controls (i.e. normal controls, including CustomControls which just inherit from them) are considered to be lookless, they normally provide a default template but their key aspect is their functionality, if you think of a <code>ComboBox</code> for example, it's a control which contains items, it should have one or no selected element and there should be a dropdown which displays the items and supports selection etc. It is the responsibility of the control to provide the necessary properties and methods which make up this functionality. Since this sometimes requires certain visual elements to exist controls can define <a href="http://msdn.microsoft.com/en-us/library/system.windows.templatepartattribute.aspx" rel="nofollow noreferrer">parts</a> which represent the minimal interface to the front-end.</p> <p>Have a look at the <a href="http://msdn.microsoft.com/en-us/library/ms745025.aspx" rel="nofollow noreferrer">Control Authoring overview</a>, it is more in-depth and probably explains a lot of things better than i can.</p> <hr> <p>Also: The ultimate lightweight method is only using Templates:</p> <pre><code>&lt;!-- You can use any resource dictionary, specifying it in App.xaml simply makes it usable all over the application. --&gt; &lt;Application.Resources&gt; &lt;ControlTemplate x:Key="CustomBorderTemplate" TargetType="{x:Type ContentControl}"&gt; &lt;Border BorderThickness="1" CornerRadius="10" BorderBrush="Red" Background="Pink" Padding="5"&gt; &lt;Border BorderThickness="1" CornerRadius="8" BorderBrush="Blue" Background="LightBlue" Padding="5"&gt; &lt;ContentPresenter Content="{TemplateBinding Content}" /&gt; &lt;/Border&gt; &lt;/Border&gt; &lt;/ControlTemplate&gt; &lt;/Application.Resources&gt; </code></pre> <pre><code>&lt;ContentControl Template="{StaticResource CustomBorderTemplate}"&gt; &lt;TextBlock Text="Lorem Ipsum"/&gt; &lt;/ContentControl&gt; </code></pre> <p>This yields the same result as the UserControl.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload