title: Dynamic template wpf
body: <p>i have made a template that look like this :</p>
  
  <pre><code>&lt;ControlTemplate x:Key="onoffValue" TargetType="{x:Type Control}"&gt;
              &lt;StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Height="20" Margin="0,5,0,0"&gt;
                  &lt;RadioButton Content="On" Height="20" Name="On_radiobutton" /&gt;
                  &lt;RadioButton Content="Off" Height="20" Name="Off_radiobutton" Margin="20,0,0,0" /&gt;
              &lt;/StackPanel&gt;
     &lt;ControlTemplate.Triggers&gt;
                  &lt;DataTrigger Binding="{Binding Path=BootSector}" Value="true"&gt;
                      &lt;Setter TargetName="On_radiobutton" Property="IsChecked" Value="true"/&gt;
                  &lt;/DataTrigger&gt;
                  &lt;DataTrigger Binding="{Binding Path=BootSector}" Value="false"&gt;
                      &lt;Setter TargetName="Off_radiobutton" Property="IsChecked" Value="true"/&gt;
                  &lt;/DataTrigger&gt;
     &lt;/ControlTemplate.Triggers&gt;
  &lt;/ControlTemplate&gt;
  </code></pre>
  
  <p>For now, it is bind to the property BootSector(bool) ofa "Configuration" object.</p>
  
  <p>I use this template in my window that has a configuration object as data context like this :</p>
  
  <pre><code>&lt;Control Template="{StaticResource onoffValue}"&gt;
  
  &lt;/Control&gt;
  </code></pre>
  
  <p>It works great, but i want to go further.</p>
  
  <p>I would like to know how i can pass a different property to my template to dynamically bind (dynamically change the property the template is bind to)
  ie i tryed something like </p>
  
  <pre><code>&lt;Control Template="{StaticResource onoffValue}" xmlns:test="{Binding Path=BootSector}"/&gt;
  </code></pre>
  
  <p>and bind it in the template to "test" but it doesn't work</p>
  
  <p>Is it possible ? How can i do that ? I think i'm not too far away but not there still !</p>
  
  <p>Thank you in advance</p>
  
  <p>Edit : Concerning Dmitry answer :<br>
      There is a bug using that. When i do :    </p>
  
  <pre><code>&lt;StackPanel local:ToggleControl.IsOn="{Binding BootSector, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                  Grid.Row="0" Grid.Column="1"
          Orientation="Horizontal" HorizontalAlignment="Center" Margin="5"&gt;
                              &lt;RadioButton Content="On" local:ToggleControl.Role="On"  Height="20" Margin="5" /&gt;
                              &lt;RadioButton Content="Off" local:ToggleControl.Role="Off" Height="20" Margin="5" /&gt;
                          &lt;/StackPanel&gt;  
  </code></pre>
  
  <p>By default BootSector is on false. When i click on the on button (true), it sets bootSector to true and then immediately to false . The behaviour should be that it stays to true until it is unchecked ? Is this related to the problem related here ? <a href="http://geekswithblogs.net/claraoscura/archive/2008/10/17/125901.aspx" rel="nofollow">http://geekswithblogs.net/claraoscura/archive/2008/10/17/125901.aspx</a></p>