title: Adding a custom dependency property to a Control Template in XAML
body: <p>I have managed to get further with my read only check box after a bit of a break and now have the functionality I want in a reasonably elegant form. The problem is I have used a bit of a hack to make it work, although this is not a disaster it would be nice to do it better.</p>
  
  <p>To recap: I want a regular looking checkbox that does not self check when it is clicked, instead the click event triggers a background worker that later on causes a variable to be updated. This variable is bound to checkbox.ischecked and it is then updated with the new value.</p>
  
  <p>I would like to use a control template based on the idea here:</p>
  
  <p><a href="https://stackoverflow.com/questions/921921/a-read-only-checkbox-in-c-wpf">A read-only CheckBox in C# WPF</a></p>
  
  <p>I have modified this and stripped out stuff I thought I didn't need (perhaps unwisely) and ended up with:</p>
  
  <pre><code>&lt;ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"&gt;
  &lt;!-- --&gt;
  &lt;Style x:Key="ReadOnlyCheckBoxStyle" TargetType="{x:Type CheckBox}" &gt;
          &lt;Setter Property="Control.Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type CheckBox}"&gt;
                  &lt;BulletDecorator SnapsToDevicePixels="true" Background="Transparent"&gt;
                      &lt;BulletDecorator.Bullet&gt;
                          &lt;Microsoft_Windows_Themes:BulletChrome Background="{TemplateBinding Background}"
                                                                 BorderBrush="{TemplateBinding BorderBrush}"
                                                                 RenderMouseOver="{TemplateBinding IsMouseOver}"
                                                                 IsChecked="{TemplateBinding Tag}"&gt;
                          &lt;/Microsoft_Windows_Themes:BulletChrome&gt;
                      &lt;/BulletDecorator.Bullet&gt;
                      &lt;ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                        HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                        Margin="{TemplateBinding Padding}"
                                        VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                        RecognizesAccessKey="True" /&gt;
                  &lt;/BulletDecorator&gt;
                  &lt;ControlTemplate.Triggers&gt;
                      &lt;Trigger Property="IsEnabled" Value="false"&gt;
                          &lt;Setter Property="Foreground" Value="{StaticResource {x:Static SystemColors.GrayTextBrushKey}}" /&gt;
                      &lt;/Trigger&gt;
                  &lt;/ControlTemplate.Triggers&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p></p>
  
  <p>This checkbox works as described above and I call it like this:</p>
  
  <pre><code>&lt;CheckBox x:Name="uiComboBox" Content="Does not set the backing property, but responds to it." 
                    Style="{StaticResource ReadOnlyCheckBoxStyle}" Tag="{Binding MyBoolean}" Click="uiComboBox_Click"/&gt;
  </code></pre>
  
  <p>The hack I made was to use the 'Tag' DependencyProperty to carry the data binding into the control template. This bypasses whatever mechanism is normally causing the checkbox to self check. To revert to a normal acting checkbox just change binding to Tag to a binding to IsChecked and inside the BulletDecorator set the TemplateBinding to IsChecked instead of Tag.</p>
  
  <p>So I guess my questions are:</p>
  
  <ol>
  <li>Have I got the wrong end of the stick? Is there a place where I can override whatever mechanism causes the box to self check? Perhaps in ControlTemplate Triggers?</li>
  <li>Is it a good idea to go around eliminating any spare XAML that I think is just being brought in from the default CheckBox or should I try and keep a complete replacement for all styles?</li>
  <li>If what I am doing is not too crazy, can I add a dependency property in XAML so that I don't have to use the Tag property?</li>
  <li>It also occurs to me that perhaps what I really want is a button control that looks like a checkbox, maybe an invisible button with the usual animated checkbox on top which I bind data to the graphic of. Any thoughts on that plan would also be very welcome.</li>
  </ol>
  
  <p>Thanks very much</p>
  
  <p>Ed</p>