How to create a common style so that I can use it throughout my application?

I searched here for SO and that I could not find a clear answer explaining how to set up a โ€œstyle resourceโ€. In my case, my dialog controls several buttons and lists and other various controls for which I want to set a common theme / style. Just as you will use a CSS file in HTML.

For simplicity, in this example, I have a style that I want to use on all of my buttons. However, I would prefer not to include all of these style resources in the xaml of my user interface layout. I would like to move the styles to a common xaml resource file that will only contain styles, so I could also easily refer to them in other wpf dialogs throughout the tool.

How to configure this to use a shared resource file containing styles for various controls in my tool? Then be able to reference and use this style in my xaml user interface.

thanks

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="200" Width="200">

    <Window.Resources>
        <Style TargetType="Button" x:Key="CoolButton" >
            <Setter Property="Margin" Value="1,2,1,2"/>
            <Setter Property="Background" Value="LightBlue"/>
            <Setter Property="HorizontalAlignment" Value="Left"/>
            <Setter Property="FontSize" Value="12" />
            <Setter Property="FontFamily" Value="Calibri" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Border Background="{TemplateBinding Background}"
                    BorderBrush="Lavender" BorderThickness="5" CornerRadius="6,0,6,0" x:Name="bd">
                            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                    Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
                    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" RecognizesAccessKey="True" />
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="bd" Property="Background" Value="LightGray"/>
                                <Setter Property="Foreground" Value="White" />
                                <Setter Property="Cursor" Value="Hand" />
                            </Trigger>

                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>

    <StackPanel>
        <Button Style="{StaticResource CoolButton}" Content="Button" Margin="2" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75"/>
        <Button Style="{StaticResource CoolButton}" Content="Button" Margin="2" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75"/>
        <CheckBox Margin="2" Content="Great"></CheckBox>

    </StackPanel>
</Window>

On the other hand, why doesn't it work to use variables for colors in the style of the xaml resource?

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

        <!--COLORS-->
        <Color x:Key="AccentColor">#CC4021</Color>


        <Style TargetType="Button">
            <Setter Property="Foreground" Value="{StaticResource AccentColor}"/>
        </Style>

</ResourceDictionary>
+4
source share
1 answer

There are several steps to follow in creating an instance-specific style, a general style.

  • Remove Key. This will allow you to use a style for each button:

    <Style TargetType="Button">
    
  • Move it to the resource file, for example Default.xaml:

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    >
        <Style TargetType="Button">
            ...
        </Style>
    </ResourceDictionary>
    
  • , App.xaml, . App.xaml , :

    <Application x:Class="..."
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <Application.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="Default.xaml" />                 
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Application.Resources>
    </Application>
    
+4

Source: https://habr.com/ru/post/1606951/


All Articles