In WPF, when styling a control, Blend has a very handy feature which allows you to get at the default style and template for any control, simply by right clicking the control and selecting "Edit Control Parts (Template)" and then selecting "Edit a Copy..."  These styles and control templates are great places to start when restyling controls.

Unfortunately, Silverlight 2 doesn't have the corrallary in Blend (yet).  However, this is easy to get around once you get a hold of the generic.xaml file for all the Silverlight controls. I went ahead and posted it, although there are two other places you can get an "official" copy. First is the source code for the Silverlight Controls themselves.  Second is by using Reflector, loading C:\Program Files\Microsoft SDKs\Silverlight\v2.0\Libraries\Client\System.Windows.Controls.dll and looking at its resources in the disassembler.

Once you have generic.xaml, restyling controls by tweaking the existing styles is simple.  First, cut/paste the contents of generic.xaml (either all the styles or just the style of the control you want to restyle) into the resources of either app.xaml or one of your pages (depending on the scope you are seeking.) Second, give the style an x:Key value so that you can reference it later. Finally, add that style attribute to your control in XAML: Style="{StaticResource CustomButton}".  That's it!  Now you can tweak the style of the button, even changing the storyboards that get fired as a result of different states.

Posted on April 1, 2008 09:55
Actions: E-mail | Comments (4)

Comments


Josh Santangelo

Josh Santangelo

March 30, 2008 09:43

blogs.msdn.com/.../...available-via-clickonce.aspx">This</a> is useful too.
          


Michael Sync

Michael Sync

April 1, 2008 21:08

>>Style="{StaticResource CustomButton}

I don't want to define "Style" to control. It would be great if the style is able to overide the exiting based on the scope..

For example:

In generic.xaml, there is a style like below for DataGrid.

<Style TargetType="localLaughingataGrid" x:Key="myDataGridStyle">

I copied this style and pasted in App.xaml in my project.
then, I change some color in that style..

When I run the application, Datagrid is not using the style from App.xaml of my project but generic.xaml from original project. Frown

Yes. Of course, If I give x:Key and use it in Datagrid, it will work but this is not the way that I want.
          


Michael Sync

Michael Sync

April 2, 2008 02:08

>>Style="{StaticResource CustomButton}

I don't want to define "Style" to control. It would be great if the style is able to overide the exiting based on the scope..

For example:

In generic.xaml, there is a style like below for DataGrid.

<Style TargetType="localLaughingataGrid" x:Key="myDataGridStyle">

I copied this style and pasted in App.xaml in my project.
then, I change some color in that style..

When I run the application, Datagrid is not using the style from App.xaml of my project but generic.xaml from original project. Frown

Yes. Of course, If I give x:Key and use it in Datagrid, it will work but this is not the way that I want.
          


Karsten Januszewski

Karsten Januszewski

April 6, 2008 06:25

Check out this post by Jaime on default styles: blogs.msdn.com/.../...tyling-and-generic-xaml.aspx" rel="nofollow">blogs.msdn.com/.../...tyling-and-generic-xaml.aspx" target="_blank" >blogs.msdn.com/.../...tyling-and-generic-xaml.aspx" rel="nofollow">blogs.msdn.com/.../...tyling-and-generic-xaml.aspx
          


Add comment

Enter your name, handle, alias, or email.

We'll incarnate your avatar from the services below.