Benutzer:MovGP0/WPF/Theming
< Benutzer:MovGP0 | WPF
MovGP0 | Über mich | Hilfen | Artikel | Weblinks | Literatur | Zitate | Notizen | Programmierung | MSCert | Physik |
Theming[Bearbeiten | Quelltext bearbeiten]
Theme Files[Bearbeiten | Quelltext bearbeiten]
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 [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")]
Styles[Bearbeiten | Quelltext bearbeiten]Explicit[Bearbeiten | Quelltext bearbeiten]
<Style x:Key="CustomStyleKey" TargetType="{x:Type local:CustomButton}">
<!-- ... -->
</Style>
<local:CustomButton Style="{StaticResource CustomStyleKey}" />
Implicit[Bearbeiten | Quelltext bearbeiten]
<Style TargetType="{x:Type local:CustomButton}">
<!-- ... -->
</Style>
BasedOn[Bearbeiten | Quelltext bearbeiten]
<Style TargetType="{x:Type local:CustomButton}"
BasedOn="{StaticResource {x:Type Button}}">
<!-- ... -->
</Style>
Example[Bearbeiten | Quelltext bearbeiten]
<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>
<ResourceDictionary (...)>
<SolidColorBrush x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:MyControl}, ResourceId=CommonBrush}"
Color="Blue">
</ResourceDictionary>
// 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]
<ResourceDictionary (...)>
<SolidColorBrush x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:MyControl}, ResourceId=CommonBrush}"
Color="White">
</ResourceDictionary>
<ResourceDictionary (...)>
<Style TargetType="{x:Type local:MyControl}">
<Setter Property="Background" Value="{DynamicResource CommonBrush}" />
<Setter Property="Template">
<TextBox Background="{TemplateBinding Background}" />
</Setter>
</Style>
</ResourceDictionary>
<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]
<ResourceDictionary (...)>
<SolidColorBrush x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:MyControl}, ResourceId=CommonBrush}"
Color="White">
</ResourceDictionary>
public static class SharedResources
{
public ComponentResourceKey { get; } = new ComponentResourceKey(typeof(MyControl), "CommonBrush");
}
<ResourceDictionary (...)>
<SolidColorBrush x:Key="{x:Static local:SharedResources.ComponentResourceKey}"
Color="White">
</ResourceDictionary>
|