Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>ContentControls &amp; ItemsControls are good for this, you can bind them to a property of your UserControl or expose them.</p> <p>Using a ContentControl (for placeholders in multiple disconnected places):</p> <pre><code>&lt;UserControl x:Class="Test.UserControls.MyUserControl2" 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;Grid&gt; &lt;Button&gt;Just a button&lt;/Button&gt; &lt;ContentControl Content="{Binding PlaceHolder1, ElementName=control}"/&gt; &lt;/Grid&gt; &lt;/UserControl&gt; </code></pre> <pre><code>public partial class MyUserControl2 : UserControl { public static readonly DependencyProperty PlaceHolder1Property = DependencyProperty.Register("PlaceHolder1", typeof(object), typeof(MyUserControl2), new UIPropertyMetadata(null)); public object PlaceHolder1 { get { return (object)GetValue(PlaceHolder1Property); } set { SetValue(PlaceHolder1Property, value); } } public MyUserControl2() { InitializeComponent(); } } </code></pre> <pre><code>&lt;uc:MyUserControl2&gt; &lt;uc:MyUserControl2.PlaceHolder1&gt; &lt;TextBlock Text="Test"/&gt; &lt;/uc:MyUserControl2.PlaceHolder1&gt; &lt;/uc:MyUserControl2&gt; </code></pre> <hr> <p>ItemsControl-Version (for collections in one place)</p> <pre><code>&lt;UserControl x:Class="Test.UserControls.MyUserControl2" 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;Grid&gt; &lt;Button&gt;Just a button&lt;/Button&gt; &lt;ItemsControl Name="_itemsControl" ItemsSource="{Binding ItemsSource, ElementName=control}"/&gt; &lt;/Grid&gt; &lt;/UserControl&gt; </code></pre> <pre><code>[ContentProperty("Items")] public partial class MyUserControl2 : UserControl { public static readonly DependencyProperty ItemsSourceProperty = ItemsControl.ItemsSourceProperty.AddOwner(typeof(MyUserControl2)); public IEnumerable ItemsSource { get { return (IEnumerable)GetValue(ItemsSourceProperty); } set { SetValue(ItemsSourceProperty, value); } } public ItemCollection Items { get { return _itemsControl.Items; } } public MyUserControl2() { InitializeComponent(); } } </code></pre> <pre><code>&lt;uc:MyUserControl2&gt; &lt;TextBlock Text="Test"/&gt; &lt;TextBlock Text="Test"/&gt; &lt;/uc:MyUserControl2&gt; </code></pre> <p>With UserControls you can decide to expose certain properties of internal controls; besides the <code>ItemsSource</code> one probably would want to also expose properties like the <code>ItemsControl.ItemTemplate</code>, but it all depends on how you want to use it, if you just set the <code>Items</code> then you do not necessarily need any of that.</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