Код IT
← Каталог

Первая форма WPF — XAML, стили и шаблоны — Шаг 3 — ресурсы и стили в App.xaml

Фрагмент из «Первая форма WPF — XAML, стили и шаблоны»: Шаг 3 — ресурсы и стили в App.xaml.

XML / XAML main.xml
<Application x:Class="NoteKeeper.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <!-- Кисти приложения -->
        <SolidColorBrush x:Key="BrushBackground" Color="#F4F6F8"/>
        <SolidColorBrush x:Key="BrushSurface" Color="#FFFFFF"/>
        <SolidColorBrush x:Key="BrushBorder" Color="#D0D7DE"/>
        <SolidColorBrush x:Key="BrushAccent" Color="#2563EB"/>
        <SolidColorBrush x:Key="BrushAccentHover" Color="#1D4ED8"/>
        <SolidColorBrush x:Key="BrushText" Color="#1F2937"/>
        <SolidColorBrush x:Key="BrushMuted" Color="#6B7280"/>

        <!-- Style — общий вид TextBox -->
        <Style TargetType="TextBox">
            <Setter Property="Padding" Value="10,8"/>
            <Setter Property="FontSize" Value="14"/>
            <Setter Property="BorderBrush" Value="{StaticResource BrushBorder}"/>
            <Setter Property="Background" Value="{StaticResource BrushSurface}"/>
            <Setter Property="Foreground" Value="{StaticResource BrushText}"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="TextBox">
                        <Border Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                CornerRadius="6"
                                Padding="{TemplateBinding Padding}">
                            <ScrollViewer x:Name="PART_ContentHost"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="IsKeyboardFocused" Value="True">
                    <Setter Property="BorderBrush" Value="{StaticResource BrushAccent}"/>
                </Trigger>
            </Style.Triggers>
        </Style>

        <!-- Style + ControlTemplate — кнопка "Добавить" -->
        <Style x:Key="AccentButtonStyle" TargetType="Button">
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="FontSize" Value="14"/>
            <Setter Property="FontWeight" Value="SemiBold"/>
            <Setter Property="Padding" Value="20,10"/>
            <Setter Property="Cursor" Value="Hand"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border x:Name="Root"
                                Background="{StaticResource BrushAccent}"
                                CornerRadius="8"
                                Padding="{TemplateBinding Padding}">
                            <ContentPresenter HorizontalAlignment="Center"
                                              VerticalAlignment="Center"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="Root" Property="Background"
                                        Value="{StaticResource BrushAccentHover}"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter TargetName="Root" Property="Opacity" Value="0.45"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Application.Resources>
</Application>
<Application x:Class="NoteKeeper.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <!-- Кисти приложения -->
        <SolidColorBrush x:Key="BrushBackground" Color="#F4F6F8"/>
        <SolidColorBrush x:Key="BrushSurface" Color="#FFFFFF"/>
        <SolidColorBrush x:Key="BrushBorder" Color="#D0D7DE"/>
        <SolidColorBrush x:Key="BrushAccent" Color="#2563EB"/>
        <SolidColorBrush x:Key="BrushAccentHover" Color="#1D4ED8"/>
        <SolidColorBrush x:Key="BrushText" Color="#1F2937"/>
        <SolidColorBrush x:Key="BrushMuted" Color="#6B7280"/>

        <!-- Style — общий вид TextBox -->
        <Style TargetType="TextBox">
            <Setter Property="Padding" Value="10,8"/>
            <Setter Property="FontSize" Value="14"/>
            <Setter Property="BorderBrush" Value="{StaticResource BrushBorder}"/>
            <Setter Property="Background" Value="{StaticResource BrushSurface}"/>
            <Setter Property="Foreground" Value="{StaticResource BrushText}"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="TextBox">
                        <Border Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                CornerRadius="6"
                                Padding="{TemplateBinding Padding}">
                            <ScrollViewer x:Name="PART_ContentHost"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="IsKeyboardFocused" Value="True">
                    <Setter Property="BorderBrush" Value="{StaticResource BrushAccent}"/>
                </Trigger>
            </Style.Triggers>
        </Style>

        <!-- Style + ControlTemplate — кнопка "Добавить" -->
        <Style x:Key="AccentButtonStyle" TargetType="Button">
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="FontSize" Value="14"/>
            <Setter Property="FontWeight" Value="SemiBold"/>
            <Setter Property="Padding" Value="20,10"/>
            <Setter Property="Cursor" Value="Hand"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border x:Name="Root"
                                Background="{StaticResource BrushAccent}"
                                CornerRadius="8"
                                Padding="{TemplateBinding Padding}">
                            <ContentPresenter HorizontalAlignment="Center"
                                              VerticalAlignment="Center"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="Root" Property="Background"
                                        Value="{StaticResource BrushAccentHover}"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter TargetName="Root" Property="Opacity" Value="0.45"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Application.Resources>
</Application>