Hi. I encountered such a problem. There is a user control with a given ControlTemplate

<ControlTemplate TargetType="local:Tria"> <Border Name="CellBorder" RenderTransformOrigin="0.5, 0.5" BorderBrush="{StaticResource BorderBrush}" BorderThickness="2"> <Border.Background> <SolidColorBrush x:Name="CellColor" Color="PaleVioletRed"/> </Border.Background> <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/> </Border> <ControlTemplate.Triggers> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Background"> <Setter.Value> <SolidColorBrush Color="Green"/> </Setter.Value> </Setter> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> 

So I want to change the back he programmatically

 private void TriaClick(object sender, MouseButtonEventArgs e) { (sender as Tria).Background = Brushes.Red; } 

but he does not change.

  • one
    In the Background property set the TemplateBinding . It looks like this: Background="{TemplateBinding Background}" - sp7
  • Thanks for the reply - Sasuke
  • Have you been helped by a variant with TemplateBinding - sp7
  • aha kk kk kk kk - Sasuke
  • Then I will issue it as an answer) - sp7

1 answer 1

In the ControlTemplate.Background property, set the TemplateBinding . In code, it looks like this:

 Background="{TemplateBinding Background}"