Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think Jon's given you the answer. However you might want to consider using a Templated Control based on the <code>ContentControl</code> rather than a <code>UserControl</code>. Here is a starter for 10. </p> <p>Add a new Silverlight Templated Control item to your project call its "LabeledControl".</p> <p>Modify the .cs file create so that it looks like this:-</p> <pre><code>public class LabeledControl : ContentControl { public LabeledControl() { this.DefaultStyleKey = typeof(LabeledControl); } #region public string Label public string Label { get { return GetValue(LabelProperty) as string; } set { SetValue(LabelProperty, value); } } public static readonly DependencyProperty LabelProperty = DependencyProperty.Register( "Label", typeof(string), typeof(LabeledControl), new PropertyMetadata(null)); #endregion public string Label } </code></pre> <p>You'll note all that we have done is change the class being inherited to <code>ContentControl</code> and added a "Label" dependency property.</p> <p>Now open the Themes/Generic.xaml file and find the default style for this control. Replace it with this:-</p> <pre><code>&lt;Style TargetType="local:LabeledControl"&gt; &lt;Setter Property="Foreground" Value="#FF000000"/&gt; &lt;Setter Property="HorizontalContentAlignment" Value="Left"/&gt; &lt;Setter Property="VerticalContentAlignment" Value="Top"/&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="local:LabeledControl"&gt; &lt;Grid&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="Auto" /&gt; &lt;RowDefinition Height="*" /&gt; &lt;/Grid.RowDefinitions&gt; &lt;TextBlock Text="{TemplateBinding Label}" /&gt; &lt;ContentPresenter Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" Cursor="{TemplateBinding Cursor}" Margin="{TemplateBinding Padding}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/&gt; &lt;/Grid&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; </code></pre> <p>All we've done here is copied the original style for <code>ContentControl</code> and wrapped it up with a <code>Grid</code> so we can place a <code>TextBlock</code> that we bind to the <code>Label</code> property.</p> <p>You will probably have noted by now that none of this actually solves the "problem" you wanted to solve. I'm just pointing out a much more "Silverlight-esq" approach to creating composite controls, its more flexible.</p> <p>To your specific problem I would say its not actually a problem, in Jon's last sample he shows how you can construct your code to avoid duplication. With the above control that code would look like:-</p> <pre><code>var lc = new LabeledControl { Label = "Check this:", Content = new CheckBox {IsChecked = True} }; </code></pre> <p>OR in Xaml:-</p> <pre><code>&lt;local:LabeledControl Label="Check this:"&gt; &lt;CheckBox IsChecked="True" /&gt; &lt;/local&gt; </code></pre> <p>As vcsjones points out to Jon using using a Generic class is not a very Silverlight friendly approach, I would avoid that. </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