How WPF-- set global style for the project.

    In the project, the need for all Button, TextBox set a default global style, one set of the same style as the number of controls is clearly unwise. Resources can set global style, there are two main ways in WPF:

1. The first is the first written style button, do not write Key, and then referenced in the App.xaml.

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style  TargetType="{x:Type CheckBox}" />
</ResourceDictionary>
          <! -   Default Button Style   -> 
              <ResourceDictionary Source = "Pack: // the Application: ,,, / Resources / ButtonStyle / BasicButton.xaml" /> 
                <! -   default TextBox style   -> 
                <ResourceDictionary Source = " Pack: // the Application: ,,, / Resources / TextBoxStyleBasic / TextBoxStyleBasic.xaml "/> 
                ! <-   default CheckBox style   -> 
            <ResourceDictionary Source =" Pack: // the Application: ,,, / Resources / ButtonStyle / BasicCheckbox .xaml "/> 
                ! <-   default scroll bar style   ->                
      <ResourceDictionary Source =" Pack: // the Application: ,,, / Resources / ControlStyle / ScrollViewBasic.xaml "/>

In this way the number of controls you need to assemble tired of how many references in the APP, the clutter will make redundant configuration file, and the second method because the default style is not Key, control is not flexible enough, so then the next.

2.

Almost writing style for the control and above, just add Key.

 <Style x:Key="DefaultCheckBox" TargetType="{x:Type CheckBox}" />

Create a new resource, unified management of all the resources of control styles. By BaseOn succession with Key's style, converted to the default global style, and then only need to reference a resource file which can in App. Even though the need to write dozens of hundreds of styles, APP also only need one line of code.

  <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="pack://application:,,,/Resources/ButtonStyle/BasicButton.xaml" />
        <ResourceDictionary Source="pack://application:,,,/Resources/ButtonStyle/BasicCheckbox.xaml" />
        <ResourceDictionary Source="pack://application:,,,/Resources/ControlStyle/ScrollViewBasic.xaml" />
        <ResourceDictionary Source="pack://application:,,,/Resources/TextBoxStyleBasic/TextBoxStyleBasic.xaml" />
    </ResourceDictionary.MergedDictionaries>
    <Style BasedOn="{StaticResource DefaultButton}" TargetType="Button" />
    <Style BasedOn="{StaticResource DefaultCheckBox}" TargetType="CheckBox" />
    <Style BasedOn="{StaticResource DefaultScrollViewer}" TargetType="ScrollViewer" />
    <Style BasedOn="{StaticResource DefaultTextBox}" TargetType="TextBox" />
</ResourceDictionary>

App in:

 <ResourceDictionary Source="pack://application:,,,/Resources/OverwrideDefaultControlStyles.xaml" />

 

Summary: If you only need to set up a global styles both a control, first you can set multiple control styles, it is recommended the second. Also: In APP.xaml, the same style controls, in reference to the bottom of a higher priority.

 

 

Guess you like

Origin www.cnblogs.com/king10086/p/11855118.html