Tuesday, March 29, 2016

Set up Custom Control for Theme and Prefix AutoGen


Theme vs Skin --- Window build in Aero2.NormalColor.xaml, Classic.Xaml
Skin need code or xaml to load from xmal file to a scope
    <Window.Resources>
        <ResourceDictionary Source=""></ResourceDictionary>
    </Window.Resources>

Style in Custom Control theme must be referenced by Component Res key
This style only applied inside Control never outside so this is not skinning

Classic.xaml/Areo.NormalColor.xaml under Theme dir
    <Style  TargetType="{x:Type Border}"
            x:Key="{ComponentResourceKey
                 TypeInTargetAssembly={x:Type loc:CustomControl1},
                 ResourceId={x:Type Border}}">
        <Setter Property="Background" Value="Red"></Setter>
    </Style>

Generic.xaml use the style by Compo key

<Border  Style="{DynamicResource {ComponentResourceKey
           TypeInTargetAssembly={x:Type local:CustomControl1},
           ResourceId={x:Type Border}}}">
   </Border>

Note Microsoft build-in Theme applied to OS elements, Window Chrome, etc, not WPF
so no key needed. Windows theme only affect WPF Aero, Areo2, Classic, Lunar...
Control  Panel..Personalization theme change

need Assembly Attribute to locate theme
[assembly:ThemeInfo(
    ResourceDictionaryLocation.SourceAssembly, // theme
    ResourceDictionaryLocation.SourceAssembly // generic
)]

design drag from Toolbox auto gene prefix
[assembly:XmlnsPrefix("http://jqd/","wpf")]
[assembly: XmlnsDefinition("http://jqd/", "WpfCustomControlLibrary1")]

to avoid verbose Component key use static class
public static  class ShareResources {
 static ComponentResourceKey _style1Key = new ComponentResourceKey
        (typeof(CustomControl1), "style1");
        public static ComponentResourceKey Style1Key
        { get { return _style1Key; }}}
<Border Style="{x:Static local:ShareResources.Style1Key}" />

No comments:

Post a Comment