WPF background Settings XAML control style System.Windows.Style

Source: Internet
Author: User

WPF background Settings XAML control style System.Windows.Style

Excerpt-From: Thank you Author: it creeps http://3w.suchso.com/projecteac-tual/wpf-zhishi-houtai-shezhi-style.html

Style MyStyle = (style) this. FindResource ("Tabitemstyle");//tabitemstyle this style is the name of the style in the referenced resource file

A static resource determines its object or value after it is first compiled and cannot be modified afterwards. A dynamic resource is determined at run time to find its value in the resource target when it is really needed during the run.

Like what:

<canvas background= "{DynamicResource Innerlgbresource}" >

Staticresources for Applications:
(1) You no longer need to modify the value of a resource after the resource is first referenced.
(2) Resource references are not recalculated based on the behavior of the runtime, such as when reloading Page/window.
(3) Use StaticResource when the property you want to set is not a DependencyObject or freezable type.
(4) Use StaticResource when you need to compile a resource into a DLL and package it as part of a program, or if you want to share it across applications.
(5) When you need to create a theme for a custom control and use resources in theme, you need to use StaticResource. Because the StaticResource resource looks for behavior when it is predictable, and itself is contained in theme. For DynamicResource, even if the resource is defined in theme, it can only wait for the runtime to be determined, leading to something that might not be expected to happen.
(6) When you need to use a resource to set a large number of dependency properties (Dependency property).
Because dependency properties have a value caching mechanism provided by the property system, if you can set the value of a dependency property when the program loads, the dependency property does not need to check its own value and return the last valid value.

The Dynamic resource is generally used in the following situations:
(1) The value of a resource depends on some conditions, and the condition is not determined until run time.
Includes system resources, or resources that can be set by the user. For example, you can create reference system properties such as systemcolors,systemfonts to set values that are dynamic, and whose values come from both the running environment and the operating system.
(2) Reference or create a theme Style for a custom control.
(3) When you want to adjust the contents of a resource dictionary while the program is running.
(4) When you want resources to be referenced forward (as in the canvas, refer to innerlgbresource )
(5) The resource file is large and you want to load it at run time.
(6) The value of the style to be created may come from other values that depend on the theme or user's settings.
(7) When the parent element of the element that references the resource is likely to change at run time, it also needs to use dynamic resources. Because the parent element changes will result in the scope of the resource query.

1. The code in the foreground Mywindow.xaml file

<tabcontrol x:name= "Menutab" grid.rowspan= "2" margin= "0" style= "{DynamicResource Tabcontrolstyle}" Grid.Row= "1" Background= "{x:null}" >                <tabitem header= "System settings" height= "margin=", "80,0,0,0" width= "" style= "{ DynamicResource Tabitemstyle} ">                    <TabItem.Background>                        <imagebrush imagesource=" Skin/ico/ico_ Dsmain.png "/>  <!--Here the picture needs to be replaced to run properly-                    </TabItem.Background>                    <grid background=" { DynamicResource MyBrush} "/>                </TabItem>             </TabControl>

2. The code in the background MyWindow.xaml.cs file

private void Button_Click (object sender, RoutedEventArgs e) {            //Dynamically add submenu            TabItem mydnymictab = new TabItem () { Header = "User Management", Height = $, Width = " n";             Set            the picture ImageBrush myimagebrush=new ImageBrush (New BitmapImage (@). /.. /skin/ico/ico_plugincleaner.png ", urikind.relative)));            mydnymictab.background=Myimagebrush;            Set position            Thickness mythickness =new Thickness (120,0,0,0);            mydnymictab.margin=mythickness;//styling style style MyStyle = (style) this. FindResource ("Tabitemstyle");//tabitemstyle this style is the name of the style in the referenced resource file Mydnymictab.style = MyStyle; Add TabItem to TabControl in menuTab.Items.Add (mydnymictab); menutab.selecteditem = mydnymictab;}   

3. Add style dictionary file reference in App.xaml

<Application.Resources>    <ResourceDictionary>        <ResourceDictionary.MergedDictionaries>            <!--App.xaml resource style--            <resourcedictionary source= "Tabcontrolstyle.xaml"/>        </ Resourcedictionary.mergeddictionaries>    </ResourceDictionary></Application.Resources>

4. Style in resource file Tabcontrolstyle.xaml:

<!--should define resource dictionary entries here. --<style x:key= "Tabcontrolstyle" targettype= "{x:type TabControl}" > <setter property= "Foreground" value= "{DynamicResource {x:static Systemcolors.controltextbrushkey}}"/> <setter property= "Padding" Value= "4,4,4,4"/ > <setter property= "borderthickness" value= "1"/> <setter property= "BorderBrush" Value= "{StaticResource Tabcontrolnormalborderbrush} "/> <setter property=" Background "value=" #F9F9F9 "/> <setter property="  Horizontalcontentalignment "value=" center "/> <setter property=" verticalcontentalignment "Value=" center "/>     <setter property= "Template" > <Setter.Value> <controltemplate targettype= "{x:type TabControl}" > <grid cliptobounds= "true" snapstodevicepixels= "true" keyboardnavigation.tabnavigation= "Local" > < grid.columndefinitions> <columndefinition x:name= "ColumnDefinition0" width= "0.192*"/> <columndefi Nition x:name= "ColumnDefinition1" width= "0.808* "/> </Grid.ColumnDefinitions> <border x:name=" Contentpanel "borderbrush=" {TemplateBinding BORDERBR Ush} "borderthickness=" {TemplateBinding borderthickness} "background=" {TemplateBinding Background} "grid.column=" 1 " keyboardnavigation.directionalnavigation= "Contained" grid.row= "0" keyboardnavigation.tabindex= "2" keyboardnavigation.tabnavigation= "Local" grid.columnspan= "1" grid.rowspan= "1" width= "Auto" margin= "0" > <Conte Ntpresenter x:name= "PART_SelectedContentHost" contentsource= "SelectedContent" snapstodevicepixels= "{ TemplateBinding snapstodevicepixels} "width=" Auto "margin=" 0 "/> </Border> <stackpanel x:name=" Header Panel "margin=" 0 "isitemshost=" True "> <StackPanel.Background> <lineargradientbrush endpoint=" 0.5, 1 "startpoint=" 0.5,0 "> <gradientstop color=" #7AFFFFFF "offset=" 0 "/> <gradientstop color=" #42F0 FCFF "offset=" 1 "/> </LinearGradientBrush> </stackpanEl. background> </StackPanel> </Grid> <ControlTemplate.Triggers> <trigger property= " IsEnabled "value=" false "> <setter property=" Foreground "value=" {dynamicresource {x:static Systemcolors.graytex Tbrushkey}} "/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </setter.va lue> </Setter> </style><style x:key= "Tabitemstyle" targettype= "{x:type TabItem}" > <setter Property= "FocusVisualStyle" value= "{StaticResource tabitemfocusvisual}"/> <setter property= "Foreground" Value = "Black"/> <setter property= "Padding" value= "6,1,6,1"/> <setter property= "BorderBrush" value= "{ StaticResource Tabcontrolnormalborderbrush} "/> <setter property=" Background "value=" {StaticResource Buttonnormalbackground} "/> <setter property=" horizontalcontentalignment "value=" Stretch "/> <Setter Property= "Verticalcontentalignment" value= "Stretch"/> <setter Property= "Template" > <Setter.Value> <controltemplate targettype= "{x:type TabItem}" > <grid snapst Odevicepixels= "true" > <Grid.RowDefinitions> <rowdefinition height= "0.69*"/> <rowdefinit  Ion height= "0.31*"/> </Grid.RowDefinitions> <border x:name= "Bd" borderthickness= "0" cornerradius= "3" Borderbrush= "Black" margin= "0" grid.rowspan= "2" visibility= "Hidden" > <Border.Background> <imageb Rush imagesource= "Skin/ico/toolbar_pushed.png"/> </Border.Background> </Border> <border X:name= "FG" borderthickness= "0" cornerradius= "3" borderbrush= "Black" margin= "0" grid.rowspan= "2" visibility= "Hidden" rendertransformorigin= "0.5,0.5" > <Border.RenderTransform> <TransformGroup> <scaletr ansform/> <SkewTransform/> <RotateTransform/> <TranslateTransform/> &L T;/transformgroup> </border.rendertransform> <Border.Background> <imagebrush imagesource= "SKIN/ICO/TOOLBAR_HOVER.P ng "/> </Border.Background> </Border> <textblock margin=" 0,0.333,0,3.833 "textwrapping=" W Rap "verticalalignment=" Stretch "d:layoutoverrides=" Height "grid.row=" 1 "horizontalalignment=" Center "text=" { TemplateBinding Header} "foreground=" white "/> <border x:name=" ico "borderthickness=" 0 "cornerradius=" 3 "Borderb Rush= "Black" margin= "4,4,4.25,0" grid.rowspan= "1" horizontalalignment= "center" verticalalignment= "center" width= "48 "Height=" background= "{TemplateBinding Background}" rendertransformorigin= "0.5,0.5" > <border.rendertransf orm> <TransformGroup> <ScaleTransform/> <SkewTransform/> <rotatetra      nsform/> <TranslateTransform/> </TransformGroup> </Border.RenderTransform> </Border> </Grid> <controltemplate.triggers> <trigger property= "IsMouseOver" value= "true"/> <trigger Property= "IsSelected" Value= "true" > <setter property= "Visibility" targetname= "Bd" value= "Visible"/> <setter property= "P Anel. ZIndex "Targetname=" ico "value=" 1 "/> </Trigger> <MultiTrigger> <multitrigger.conditions&        Gt       <condition property= "IsSelected" value= "false"/> <condition property= "IsMouseOver" value= "true"/> </MultiTrigger.Conditions> <setter property= "Visibility" targetname= "FG" value= "Visible"/> <se Tter property= "RenderTransform" targetname= "ico" > <Setter.Value> <TransformGroup> & Lt          ScaleTransform scalex= "1.05" scaley= "1.05"/> <SkewTransform/> <RotateTransform/> <TranslateTransform/> </TransformGroup> </Setter.Value> </Setter> </ Multitrigger> <trigger property= "IsEnabled" value= "false"/> </ControlTemplate.Triggers> </control template> </Setter.Value> </Setter> </Style>

WPF background Settings XAML control style System.Windows.Style

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.