Resourcedictionary is a dictionary of key objects.Code. You can define styles and templates to call them on other pages at any time.
First, create a resourcedictionary page, for example:
Then, write the following code to the resource dictionary file: style and template:
< 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 " > <Stackpanel orientation = " Vertical " > <Stackpanel orientation = " Horizontal " Margin = " 5 " Width = " 380 " > <Textblock text =" {Binding artname} " Style = " {Staticresource tbstyle} " Margin = " 5 10 0 0 " > </Textblock> <textbox text = " {Binding artcontent} " Name = " Tbname " Margin = " 5 " > </Textbox> <textbox text = " {Binding artauthor} " Margin = " 5 " > </Textbox> <textblock text = " {Binding artupdatetime} " Margin =" 5 10 0 0 " > </Textblock> </stackpanel> <stackpanel orientation = " Horizontal " Visibility = " Collapsed " > <Textbox text = " {Binding artcontent} " Width = " 280 " > </Textbox> </stackpanel> </datatemplate> <! -- Style --> <style X: Key = " Tbstyle " Targettype = " Textblock " > <Setter property = " Fontweight " Value = " Bold " > </Setter> <setter property = " Foreground " Value = " Red " > </Setter> </style> </resourcedictionary>
In this case, we copy the resource dictionary and only 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 resource Dictionary A in the blue font, and then load the resource dictionary B in 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. mergeddictionaries> <resourcedictionary source = " Resourcedic. XAML " /> <Resourcedictionary source = " Resourcedic2.xaml " /> </Resourcedictionary. mergeddictionaries> </resourcedictionary> </application. Resources> </Application>
There are two ways to load resources in this resource dictionary:
First Kind Of XAML direct loading, As follows:
<Grid X: Name = " Layoutroot " Background = " White " Datacontext = " {Staticresource sourcelist} " > <Textblock name =" Tblock " Style = " {Staticresource tbstyle} " Horizontalalignment = " Left " Width = " 100 " TEXT = " Today's first " > </Textblock> <ListBox X: Name = " Lbres " Itemssource = " {Binding articlelist} " Margin = " 0 50 0 0 " Itemtemplate = " {Staticresource lbtmp} " Horizontalalignment = " Left " Verticalalignment = " Top " Height = " 400 " > </ListBox> </GRID>
Second, background Loading:
PublicMainpage () {initializecomponent (); datatemplate dtemp= Application. Current. Resources ["Lbtmp"]AsDatatemplate; lbres. itemtemplate=Dtemp ;}
The effect is as follows:
From the above effect, the effective style is the second resource dictionary in red. At that time, because <resourcedictionary. mergeddictionaries> resource dictionary is merged, the resource dictionary added later will overwrite the previous key dictionary resource with the same name.
If you need the source code, click slresdic.zip to download it. Thank you ~