Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There is a HeaderedContentControl and HeaderedItemsControl in the framework already...</p> <p>But if you really want to create your own then you should probably use a TemplateBinding. Try something like this instead:</p> <pre><code>class MyHeaderedControl : ContentControl { public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register( "Header", typeof(object), typeof(MyHeaderedControl), new PropertyMetadata()); public MyHeaderedControl() { this.DefaultStyleKey = typeof(MyHeaderedControl); } } </code></pre> <p>Then in your project create a file at "\Themes\Generic.xaml". This is a specially named file and must be in the root of the project then in the Themes folder. It must contain a ResourceDictionary.</p> <pre><code>&lt;ResourceDictionary xmlns="..." xmlns:x="..." xmlns:c="MyControlLibrary1" &gt; &lt;Style TargetType="{x:Type c:MyHeaderedControl&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="{x:Type c:MyHeaderedControl}"&gt; &lt;StackPanel&gt; &lt;ContentControl Content="{TemplateBinding Header}" /&gt; &lt;ContentPresenter /&gt; &lt;/StackPanel&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;/ResourceDictionary&gt; </code></pre> <p>Also, in your AssemblyInfo.cs add this attribute if it's not there already:</p> <pre><code>[assembly: ThemeInfo(ResourceDictionaryLocation.SourceAssembly, ResourceDictionaryLocation.SourceAssembly)] </code></pre> <p>So for the overview. The general idea is to create some type of logical control where you have properties and events and logic etc. Then in the same assembly you provide default themes. That is how the controls will be displayed by default. At any place where the controls are used the default templates can be overriden and specific templates can be overridden as usual.</p> <p>So this is the most pain free way you can add custom content like this to your custom controls! Try it once and it will make sense and not feel to cludgy. If you make more controls just keep adding them to the Generic.xaml file.</p>
 

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