title: how I can modfiy Control Template WPF/C# in code behind?
body: <p>Excuse me for my poor english:</p>
  
  <p>In my application I have a ResourceDictionary " DesignerItem" where i define a Control Template "ControlTemplate x:Key="ConnectorDecoratorTemplate" TargetType="{x:Type Control}"" , this Control Template allows me  to have  points on objects of type "DesignerItem" this points allow to draw  lines between points. my ResourceDictionary is definied lik this:</p>
  
  <p></p>
  
  <pre><code>&lt;!-- Connector Style --&gt;
  &lt;!-- les Connector son les points qui nous permet des tracer les lignes--&gt;
  &lt;Style TargetType="{x:Type s:Connector}"&gt;
      &lt;Setter Property="Width" Value="8"/&gt;
      &lt;Setter Property="Height" Value="8"/&gt;
      &lt;Setter Property="Cursor" Value="Cross"/&gt;
      &lt;Setter Property="SnapsToDevicePixels" Value="true"/&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type s:Connector}"&gt;
                  &lt;Grid &gt;
                      &lt;!-- transparent extra space makes connector easier to hit --&gt;
                      &lt;!--c un carrer qui nous permet de dessiner la line entre les object--&gt;
                      &lt;Rectangle Fill="red" Margin="-2"/&gt;
                      &lt;Rectangle Fill="Red" StrokeThickness="5" Stroke="#AA000080"/&gt;
                  &lt;/Grid&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  
  
  
  
  &lt;!--
  la partie suivante c'est la partie qui me me pose un probleme,
  c'est la partie que je veux la modifer a partir du code source 
  --&gt;
  
  &lt;!-- ConnectorDecoratorTemplate Default Template --&gt;
  
  
  &lt;ControlTemplate  x:Key="ConnectorDecoratorTemplate" TargetType="{x:Type Control}"&gt;
  
      &lt;Grid x:Name="LesPointDeConnexion"&gt;&lt;!-- les point qui me permet de tracer des lignes--&gt;
              &lt;Grid.RowDefinitions&gt;
                  &lt;RowDefinition Height="0.5*"/&gt;
                  &lt;RowDefinition Height="0.5*"/&gt;
              &lt;/Grid.RowDefinitions&gt;
          &lt;s:Connector  Orientation="Left"  VerticalAlignment="Center" HorizontalAlignment="Left" Margin="5" Grid.Row="0"/&gt;
          &lt;s:Connector  Orientation="Left"  VerticalAlignment="Center" HorizontalAlignment="Left" Margin="5" Grid.Row="1"/&gt;
  
          &lt;/Grid&gt;
  &lt;/ControlTemplate&gt;
  
  
  
  
  &lt;!-- DesignerItem Style --&gt;
  &lt;Style  TargetType="{x:Type s:DesignerItem}"&gt;
      &lt;Setter Property="MinWidth" Value="10"/&gt;
      &lt;Setter Property="MinHeight" Value="10"/&gt;
      &lt;Setter Property="SnapsToDevicePixels" Value="True"/&gt;        
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type s:DesignerItem}"&gt;
                  &lt;Grid DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}"&gt;
  
  
                      &lt;!-- PART_DragThumb --&gt;
                      &lt;c:DragThumb x:Name="PART_DragThumb" 
                                   Cursor="SizeAll"/&gt;
  
                      &lt;!-- PART_ResizeDecorator --&gt;
                      &lt;Control x:Name="PART_ResizeDecorator" 
                               Visibility="Collapsed"
                               Template="{StaticResource ResizeDecoratorTemplate}"/&gt;
  
  
                      &lt;!-- PART_ContentPresenter --&gt;
                      &lt;ContentPresenter x:Name="PART_ContentPresenter"
                                        HorizontalAlignment="Stretch"
                                        VerticalAlignment="Stretch"
                                        Content="{TemplateBinding ContentControl.Content}"
                                        Margin="{TemplateBinding ContentControl.Padding}"/&gt;
  
  
  
                      &lt;!-- PART_ConnectorDecorator --&gt;
                      &lt;Control x:Name="PART_ConnectorDecorator"
                               Visibility="Visible"
                               Template="{StaticResource ConnectorDecoratorTemplate}"/&gt;
                  &lt;/Grid&gt;
                  &lt;ControlTemplate.Triggers&gt;
                      &lt;DataTrigger Value="True" Binding="{Binding RelativeSource={RelativeSource Self},Path=IsGroup}"&gt;
                          &lt;Setter TargetName="PART_DragThumb" Property="Visibility" Value="Collapsed"/&gt;
                      &lt;/DataTrigger&gt;
                      &lt;MultiDataTrigger&gt;
                          &lt;MultiDataTrigger.Conditions&gt;
                              &lt;Condition Value="True" Binding="{Binding RelativeSource={RelativeSource Self},Path=IsSelected}"/&gt;
                              &lt;Condition Value="{x:Static sys:Guid.Empty}" Binding="{Binding RelativeSource={RelativeSource Self},Path=ParentID}"/&gt;
                          &lt;/MultiDataTrigger.Conditions&gt;
                          &lt;Setter TargetName="PART_ResizeDecorator" Property="Visibility" Value="Visible"/&gt;
                      &lt;/MultiDataTrigger&gt;
                      &lt;Trigger Property="IsMouseOver" Value="true"&gt;
                          &lt;Setter TargetName="PART_ConnectorDecorator" Property="Visibility" Value="Visible"/&gt;
                      &lt;/Trigger&gt;
                      &lt;DataTrigger Value="True" Binding="{Binding RelativeSource={RelativeSource Self},Path=IsDragConnectionOver}"&gt;
                          &lt;Setter TargetName="PART_ConnectorDecorator" Property="Visibility" Value="Visible"/&gt;
                      &lt;/DataTrigger&gt;
                      &lt;DataTrigger Value="True" Binding="{Binding RelativeSource={RelativeSource Self},Path=IsGroup}"&gt;
                          &lt;Setter TargetName="PART_ConnectorDecorator" Property="Visibility" Value="Hidden"/&gt;
                      &lt;/DataTrigger&gt;
                  &lt;/ControlTemplate.Triggers&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p></p>
  
  <p>Now I want to define or modify the Control Template  "ControlTemplate x:Key="ConnectorDecoratorTemplate" TargetType="{x:Type Control}"" in code behind because the my control Template is dynamic (for some object DesigneItem i have 2 points and for others I have 4, 5, 6 or more) for this reason I want to create or modify this control template in code behind.</p>
  
  <p>please help me :(</p>