Benutzer:MovGP0/WPF/Theming

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen
   MovGP0        Über mich        Hilfen        Artikel        Weblinks        Literatur        Zitate        Notizen        Programmierung        MSCert        Physik      

  • Resource Dictonary with target set to a specific control
  • located in the applications Theme folder
  • depends on operating system; not skinning
Style Precedence
  1. Element
  2. Application
  3. Theme
File Name Definition
Themes\Generic.xaml Common resource dictionary
Themes\{ThemeName}.{ThemeColor}.xaml
Themes\Classic.xaml Windows 9x, Windows 2000, Windows XP Classic Theme
Themes\Luna.NormalColor.xaml Windows XP blue theme
Themes\Luna.Homestead.xaml Windows XP olive theme
Themes\Luna.Metallic.xaml Windows XP silver theme
Themes\Royale.NormalColor.xaml Windows XP Media Center Edition
Themes\Aero.NormalColor.xaml Windows Vista, Windows 7
Themes\Aero2.NormalColor.xaml Windows 8
  • Default themes are located in C:\Program Files (x86)\Windows Kits\10\DesignTime\CommonConfiguration\Neutral\UAP\10.0.10240.0\Generic\Generic.xaml.
  • XAML theme resources. In: MSDN. Microsoft, abgerufen am 28. Dezember 2015 (englisch).

External Theme Files

[Bearbeiten | Quelltext bearbeiten]

Usage of theme files from different library using assembly attribute:

[assembly: ThemeInfo(
    ResourceDictionaryLocation.SourceAssembly, // theme specific theme location
    ResourceDictionaryLocation.SourceAssembly)] // generic theme location

Assembly has te to named {AssemblyName}.{ThemeName}.dll and should get an XAML namespace:

[assembly: XmlnsPrefix("http://schemas.mycompany.com/wpf", "wpf")]

Hook up XAML namespace with CLR namespace:

[assembly: XmlnsDefinition("http://schemas.mycompany.com/wpf", "CustomControlLibrary.Controls.PropertyGrid")]
  • style is named has to be applied manually
Themes\Generic.xaml
<Style x:Key="CustomStyleKey" TargetType="{x:Type local:CustomButton}">
    <!-- ... -->
</Style>
Usage
<local:CustomButton Style="{StaticResource CustomStyleKey}" />
  • applies to all instances of a class
<Style TargetType="{x:Type local:CustomButton}">
    <!-- ... -->
</Style>
  • reuse style from existing control
<Style TargetType="{x:Type local:CustomButton}" 
       BasedOn="{StaticResource {x:Type Button}}">
    <!-- ... -->
</Style>
Templates/Generic.xaml
<ResourceDictionary (...)>
    <SolidColorBrush x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:MyControl}, ResourceId=CommonBrush}" 
                     Color="White">

    <Style TargetType="{x:Type local:MyControl}">
        <Setter Property="Background" Value="{DynamicResource CommonBrush}" />
        <Setter Property="Template">
            <TextBox Background="{TemplateBinding Background}" />
        </Setter>
    </Style>
</ResourceDictionary>
Templates/Aero2.NormalColor.xaml
<ResourceDictionary (...)>
    <SolidColorBrush x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:MyControl}, ResourceId=CommonBrush}"
                     Color="Blue">
</ResourceDictionary>
AssemblyInfo.cs
// use themes from current assembly
[assembly: ThemeInfo(ResourceDictionaryLocation.SourceAssembly, ResourceDictionaryLocation.SourceAssembly)]
// do not apply themes
[assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.None)]
// use theme from external assembly
[assembly: ThemeInfo(ResourceDictionaryLocation.ExternalAssembly, ResourceDictionaryLocation.ExternalAssembly)]

Merging Theme Dictionaries

[Bearbeiten | Quelltext bearbeiten]
Templates/Generic/Brushes.xaml
<ResourceDictionary (...)>
    <SolidColorBrush x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:MyControl}, ResourceId=CommonBrush}" 
                     Color="White">
</ResourceDictionary>
Templates/Generic/Controls.xaml
<ResourceDictionary (...)>
    <Style TargetType="{x:Type local:MyControl}">
        <Setter Property="Background" Value="{DynamicResource CommonBrush}" />
        <Setter Property="Template">
            <TextBox Background="{TemplateBinding Background}" />
        </Setter>
    </Style>
</ResourceDictionary>
Templates/Generic.xaml
<ResourceDictionary (...)>
    <ResourceDictionary.MergedDictionary>
        <ResourceDictionary Source="/MyLibrary;component/Themes/Generic/CommonBrushes.xaml" />
        <ResourceDictionary Source="/MyLibrary;component/Themes/Generic/Controls.xaml" />
    </ResourceDictionary.MergedDictionary>
</ResourceDictionary>

Simplifying ComponentResourceKey Bindings

[Bearbeiten | Quelltext bearbeiten]
Before
<ResourceDictionary (...)>
    <SolidColorBrush x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:MyControl}, ResourceId=CommonBrush}" 
                     Color="White">
</ResourceDictionary>
Helper
public static class SharedResources
{
    public ComponentResourceKey { get; } = new ComponentResourceKey(typeof(MyControl), "CommonBrush");
}
After
<ResourceDictionary (...)>
    <SolidColorBrush x:Key="{x:Static local:SharedResources.ComponentResourceKey}" 
                     Color="White">
</ResourceDictionary>