Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think your best solution here is to use attached properties instead of subclassing. This will allow you to have your cake and eat it too.</p> <p>Instead of writing this:</p> <pre><code>&lt;my:CustomButton ImageSource="Abc" Text="Def" ... /&gt; </code></pre> <p>you would write this:</p> <pre><code>&lt;Button my:ButtonLook.ImageSource="Abc" my:ButtonLook.Text="Def" ... /&gt; </code></pre> <p>This will work with data binding and everything. To implement this, create your "ButtonLook" class deriving from DependencyObject, and create the two attached properties using the "propa" snippet in Visual Studio. Then set a PropertyChangedCallback on each to construct update the ContentControl.Content property on whatever they are attached to.</p> <p>An alternative solution is to embed a hidden RadioButton inside your CustomRadioButton subclass, give it an empty template to make it invisible, and bind the IsChecked and GroupName properties:</p> <pre><code>&lt;ControlTemplate TargetType="my:CustomRadioButton"&gt; &lt;Grid&gt; &lt;RadioButton IsChecked="{Binding ToggleButton.IsChecked, RelativeSource={RelativeSource TemplatedParent}}" GroupName="{TemplateBinding GroupName}"&gt; &lt;RadioButton.Template&gt;&lt;ControlTemplate /&gt;&lt;/RadioButton.Template&gt; &lt;/RadioButton&gt; ... visual part here ... </code></pre> <p>Be sure that your CustomToggleButton and CustomRadioButton subclasses use AddOwner to create DependencyProperties such as IsChecked and GroupName rather than creating new ones.</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