---
title: "Silverlight动态设置样式"
author: "hao84099"
date: "2012-02-15T16:29:54Z"
tags: ["Silverlight", "样式"]
categories: ["Web前端"]
slug: "silverlight"
draft: false
lastmod: "2012-02-15T16:35:19Z"
id: 1407369
url: "https://hao84099.iteye.com/blog/1407369"
---

1.从页面资源中获取样式并应用


```C#
btnTest.Style = (Style)this.Resources["bigbuttonstyle"]
```



首先，需要创建一个ResourceDictionary对象并提供正确的URI:


```C#

ResourceDictionary dictionary = new ResourceDictionary();
dictionary.Source = new Uri(&quot;/Test.xaml&quot;,UriKind.Relative);

```



假设样式字典的名字叫Test.xaml，资源字典的编译格式必须为内容(将属性窗口的Build Action设置为Content)。
在配置好ResourceDictionary后，就可以从获取样式并应用它:
 

```C#

btnTest.Style = (Style)dictionary["smallbuttonstyle"];

```


 
3.移除样式

 
```C#

btnTest.Style = null

```