Silverlight utility tip series: 68. Resource dictionary resourcedictionary of Silverlight

Source: Internet
Author: User

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 ~

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.