WPF Resource Dictionary

In WPF applications, XAML resources are divided into Static Resources (static resources) and Dynamic Resources (dynamic resources). XAML resources can be divided into Framework Element. Resources and Application.Resources. Framework Element. Resources is the application of resource objects to different objects with the same number of objects, called page resources, which are usually defined on the XAML page root elements; Application.Resources is the application through the whole application. Level resources are usually defined on App.xaml pages. The following is Application.Resources.

The first step is to define a resource dictionary.

The effect of the above image shows that first select the application (right mouse button), then select (add) and then click (resource dictionary).

The following figure shows that it pops up the Add New Items window, chooses (Resource Dictionary (WPF), fills in the name, and then clicks (Add) to create the Resource Dictionary. If you create the Resource Dictionary successfully, you can write the Dictionary.

Here is a small case style code:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:BCClothesManage_Client">

   <Style x:Key="GuanBiIcon" TargetType="{x:Type Button}">
      <Setter Property="Template">
         <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid Background="{TemplateBinding Background}">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto"/>
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <Image Width="13" Height="29" Source="Close icon.png" Margin="2,2,0,0"  Grid.Row="0"></Image>
                    <ContentPresenter Grid.Row="1" HorizontalAlignment="Right"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="Background" Value="{x:Null}"/>
    <Setter Property="Foreground" Value="Black"/>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="#e2595f"/>
            <Setter Property="Cursor" Value="Hand"/>
        </Trigger>
    </Style.Triggers>
  </Style>
  </ResourceDictionary>

After the style is written, the second step is to merge the resource dictionary attributes, click App.xaml, and refer to the resource dictionary.

<Application x:Class="BCClothesManage_Client.App"

         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:BCClothesManage_Client"
         StartupUri="LoginWindow.xaml">
      <Application.Resources>
           <ResourceDictionary>
                   <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Dictionary2.xaml"/>
           </ResourceDictionary.MergedDictionaries>
          </ResourceDictionary>
         </Application.Resources>
         </Application>

All resource items will eventually be integrated into Resource Dictionary, that is to say, whether they are Resources of Framework Element, Resources of Window s, Resources of Application, or Resources defined in a specific Resource Dictionary, they are actually together at the time of compilation and execution of the entire application. As ergodic sets, they coexist in a relative session space. We also mentioned that Resource key s can be allowed to have the same, so that when traversing Resource Dictionary with different relative addresses, we can determine which one is valid based on the lookup behavior of StaticResource or DynamicResource. Usually for maintenance and flexibility considerations, we usually divide the Resource Dictionary file into several, but in some cases we only need to use some of these resources, then we can extract and merge resources from several separate files, add default resources to the application: in fact, the default RSEU is the default. Rce Dictionary is added to the Global Resource of Application.

Below is a button reference style code;

  <Button Style="{StaticResource GuanBiIcon}" Click="Button_Click" Height="35"  Width="29"/>

Above is the rendering, and the style code encapsulates a closed icon image.

Keywords: Session

Added by digitalecartoons on Wed, 07 Aug 2019 16:18:22 +0300