Allow reprint, please be sure to use hyperlinks in the form of the original source of the article, author information and this statement. Otherwise, the legal liability will be investigated. http://chengxingliang.blog.51cto.com/3972944/886643
ResourceDictionary is a keyed object dictionary that can be used in XAML and in code. In which we can define styles, templates, etc. to make it easy to call on other pages at any time.
First we create a new ResourceDictionary page such as:
Then write the following code to the resource dictionary file, namely the styles and templates:
<resourcedictionary xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x= "http// Schemas.microsoft.com/winfx/2006/xaml "> <!--Data template--<datatemplate x:key=" lbtmp "> <stackpane L orientation= "Vertical" > <stackpanel orientation= "Horizontal" margin= "5" width= "380" > <textblock text= "{Binding artname}" style= "{StaticResource tbstyle}" margin= "5 0 0" >& Lt;/textblock> <textbox text= "{Binding artcontent}" name= "Tbname" margin= "5" ></TextBox> <textbox text= "{Binding artauthor}" margin= "5" ></TextBox> <textblock text= "{Bindi ng Artupdatetime} "margin=" 5 0 0 "></TextBlock> </StackPanel> <stackpanel Orient ation= "Horizontal" visibility= "collapsed" > <textbox text= "{Binding artcontent}" width= "280" ></ Textbox> </STACKPAnel> </StackPanel> </DataTemplate> <!--styles-<style x:key= "Tbstyle" targettype= "T Extblock "> <setter property=" fontweight "value=" Bold "></Setter> <setter property=" Foregrou nd "value=" Red "></Setter> </Style></ResourceDictionary>
At this point, we'll copy this resource dictionary just to change the font color from blue to red in the text style. Then we load the two resource dictionaries in App.xaml, first load the Blue font resource dictionary A, and then we load the resource dictionary B of the red font, the code is as follows:
<application xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x= "http// Schemas.microsoft.com/winfx/2006/xaml " x:class=" Slresdic.app " > <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <resourcedictionary Source= "Resourcedic.xaml"/> <resourcedictionary source= "Resourcedic2.xaml"/> </ resourcedictionary.mergeddictionaries> </ResourceDictionary> </Application.Resources> </Application>
The resources in this resource dictionary are loaded in the following two ways:
The first type of XAML is loaded directly , as follows:
<grid x:name= "LayoutRoot" background= "white" datacontext= "{StaticResource SourceList}" > < TextBlock name= "Tblock" style= "{StaticResource tbstyle}" horizontalalignment= "left" width= "+" text= "Today First" > </TextBlock> <listbox x:name= "Lbres" itemssource= "{Binding articlelist}" margin= "0 0 0" Itemtemplate= "{StaticResource lbtmp}" horizontalalignment= "left" verticalalignment= "Top" height= "400" > </ListBox> </Grid>
The second type of background load :
Public MainPage () { InitializeComponent (); DataTemplate dtemp = application.current.resources["Lbtmp"] as DataTemplate; Lbres.itemtemplate = dtemp; }
The effect is as follows:
Judging from the effect above, the style style we have in effect is the second resource dictionary in red. Because of the <ResourceDictionary.MergedDictionaries> resource dictionary merge, the resource dictionary that is added later overwrites the previous key dictionary resource with the same name.
If you need source code, please click Slresdic.zip Download, thank you ~
This article is from the "Cheng Xingliang-silverlight" blog, be sure to keep this source http://chengxingliang.blog.51cto.com/3972944/886643
Silverlight Practical Tips Series: 68.Silverlight resource dictionary ResourceDictionary