There is a markup file for the button template:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:MyProj.UI.DesktopClient"> <!--Шаблон для кнопок--> <ControlTemplate x:Key="BlueButton" TargetType="{x:Type Button}"> <Border Name="Border" Background="#FF2F8FD1" BorderBrush="Black" CornerRadius="4" TextBlock.Foreground="White" TextBlock.FontWeight="Bold"> <Grid> <Rectangle Name="StrokeBorder" Stroke="Black" StrokeThickness="1" StrokeDashArray="1, 1"></Rectangle> <ContentPresenter Margin="{TemplateBinding Padding}" HorizontalAlignment="Center" VerticalAlignment="Center"></ContentPresenter> </Grid> </Border> <ControlTemplate.Triggers> <!--При наведении мыши--> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="Border" Property="Background" Value="#FF0044A6"></Setter> </Trigger> <!--При нажатии--> <Trigger Property="IsPressed" Value="True"> <Setter TargetName="Border" Property="Background" Value="#FF003A63"></Setter> <Setter TargetName="Border" Property="TextBlock.Foreground" Value="Black"></Setter> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </ResourceDictionary> 

This file is declared in App.xaml:

 <Application x:Class="MyProj.UI.DesktopClient.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:MyProj.UI.DesktopClient" StartupUri="Authorize.xaml"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <!--controltemplates--> <ResourceDictionary Source="Resources/ControlTemplates/ButtonTemplate.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </Application> 

In the window for the button I write the following code:

 <Button Content="Войти в систему" Margin="5" Template="{DynamicResource BlueButton}"/> 

Everything compiles without problems, but this button is not really visible in the window. What could be the problem? Broke the whole head.

  • I did everything that you have written in the question and I have a blue button. - ixSci
  • @ixSci, do you also have a xaml file with a template separated from the form itself? - klutch1991
  • Yes, I just added it to the project - ixSci
  • By the way, why are you using DynamicResource ? StaticResource is enough, and the advantage of it is that if a template is not found, then it will tell you about it - ixSci
  • So he tells me that the resource 'BlueButton' was not found. - klutch1991

1 answer 1

I always used StaticResource , and never had any problems. Try it.

  • StaticResource gives an XmlParseException. - klutch1991
  • @ klutch1991, then you obviously have an error somewhere in the markup, it is clear that DynamicResource was not displayed either. - Arthur Edgarov
  • @ klutch1991, look at the details of the error, there should be information from which the exception occurs. - Arthur Edgarov
  • Inner Exception indicates that the resource was not found. - klutch1991
  • @ klutch1991, I'm not exactly sure, but if I wrap your template in style, it's just the only difference from my resource dictionaries, you have a naked ControlTemplate , and I have everything in Style . - Arthur Edgarov