Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I just found out what I was doing wrong! It was right in front of me and I didn't see it. </p> <p>The issue with this problem was that I had <code>Children</code> set as a DependencyProperty - which means that it will be static and global. So the whole <code>RadioButton</code> collection was available pretty much to all the controls in the Window. (On hindsight which is probably why StackPanel, Canvas etc. doesn't have the <code>Children</code> property as a DependencyProperty). <strong>You can find more information about this <a href="http://msdn.microsoft.com/en-us/library/aa970563.aspx" rel="nofollow noreferrer">here</a>.</strong></p> <p>Thanks kek444 for posting a simpler way to do this. :D</p> <p>In order to fix this what you need to do is remove the DependencyProperty and declare Children as a normal property with a private <code>set</code>. </p> <p>I have modified the code:</p> <p><strong>CustomControl.cs</strong></p> <pre><code>using System.Collections.Generic; using System.Windows; using Sytem.Windows.Controls; using System.Windows.Markup; namespace RandomControl { [ContentProperty("Children")] public class CustomControl1 : Control { private ObservableCollection&lt;RadioButton&gt; _children; private ItemsControl _control; public ObservableCollection&lt;RadioButton&gt; Children { get { if (_children == null) _children = new ObservableCollection&lt;RadioButton&gt;(); return _children; } private set { _children = value; } } static CustomControl1() { DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl1), new FrameworkPropertyMetadata(typeof(CustomControl1))); } public override void OnApplyTemplate() { base.OnApplyTemplate(); _control = base.GetTemplateChild("PART_ItemsControl") as ItemsControl; // display the radio buttons if (_control != null) _control.ItemsSource = Children; } } } </code></pre> <p><strong>Generic.xaml</strong></p> <pre><code>&lt;ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:RandomControl"&gt; &lt;Style TargetType="{x:Type local:CustomControl1}"&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="{x:Type local:CustomControl1}"&gt; &lt;Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"&gt; &lt;ItemsControl Name="PART_ItemControl" Background="{TemplateBinding Background}"&gt; &lt;ItemsControl.ItemsPanel&gt; &lt;ItemsPanelTemplate&gt; &lt;StackPanel&gt;&lt;/StackPanel&gt; &lt;/ItemsPanelTemplate&gt; &lt;/ItemsControl.ItemsPanel&gt; &lt;/ItemsControl&gt; &lt;/Border&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;/ResourceDictionary&gt; </code></pre>
    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. 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