I recently got a comment asking if I could do something on creating a Blend-type ScollViewer styling. The only problem is that the ScrollViewer is a multi-post affair, which I’ll try to get completed in the next month or so. I’m going to go ahead and put up the basics here, much like my Styling the ComboBox and Styling the ListView posts.
In the meantime, I’m making available for download a Resource Dictionary with the Blend ScrollViewer style as I’ve approximated it. (You may have to right-click “Save As…” on that file since IE will do its darndest to open it up.) Just load the resource dictionary into your project and set
<ScrollViewer Template=”{DynamicResource BlendScrollTemplate}” />
Note: This is not the “real” Blend styles… just my rendition/approximation.
In the meantime, here’s the overview for the ScrollViewer. When you look at a template of the ScrollViewer (right-click on the ScrollViewer, got to “Edit Control Parts (Template) -> Edit a Copy…“) you should see something like this:
![clip_image001[7]](http://www.designersilverlight.com/wp-content/uploads/2008/08/clip-image0017-thumb.png)
If you want to change something about the main content area (highlighted below), you’re probably going after the PART_ScrollContentPresenter
![clip_image001[11]](http://www.designersilverlight.com/wp-content/uploads/2008/08/clip-image00111-thumb.png)
If you want to style the corner (highlighted below), look at changing the Corner Rectangle.
![clip_image001[15]](http://www.designersilverlight.com/wp-content/uploads/2008/08/clip-image00115-thumb.png)
If you want to style the HorizontalScrollBar and/or the VerticalScrollBar (highlighted below), you should right-click on either the PART_VerticalScrollBar or the PART_HorizontalScrollBar and go to “Edit Control Parts (Template) -> Edit a Copy…”
![clip_image001[13]](http://www.designersilverlight.com/wp-content/uploads/2008/08/clip-image00113-thumb.png)
A point of note: Because of the way Blend works, it can be difficult to visually style a Vertical and Horizontal ScrollBar in the same Template. Don’t create another template. It’s a waste of time and will make your resources a pain to navigate. I’ll go over exactly what to do in a little bit.
The ScrollBar template should look something like this:
![clip_image001[9]](http://www.designersilverlight.com/wp-content/uploads/2008/08/clip-image0019-thumb.png)
If you want to style the ScrollBar thumbs (the bars you would drag to scroll, highlighted below), you’ll need to change the template for the “Thumb” in the PART_Track. Note: Unless you’re doing something really complex, you should only need to style the Thumb control one time. You don’t need different styles for the vertical and the horizontal.
![clip_image001[17]](http://www.designersilverlight.com/wp-content/uploads/2008/08/clip-image00117-thumb.png)
If you want to style the directional buttons (highlighted below), you will need to change the templates for the first and last “RepeatButton” controls (the ones that aren’t in the PART_Track) using the right-click -> Edit Control Parts (Template) -> Edit Template. (This template should already be copied into your resources.) Again, unless you’re doing something complex, you should only need to style this button one time.
![clip_image001[19]](http://www.designersilverlight.com/wp-content/uploads/2008/08/clip-image00119-thumb.png)
If you want to style the empty area that allows for fast scrolling, you will need to change the style for the two RepeatButtons in the PART_Track (DecreaseRepeatButton and IncreaseRepeatButton)using the right-click -> Edit Control Parts (Template) -> Edit Template. You should only need to do this one time and then apply that style/template across the all the instances of this button.
![clip_image001[21]](http://www.designersilverlight.com/wp-content/uploads/2008/08/clip-image00121-thumb.png)
Over the next couple weeks, I’ll try to put up posts going over how to style all of these into the Blend style. I’ll update this post pointing to the more in-depth tutorials as I go along. Until I get around to doing that, feel free to download the ResourceDictionary with the Blend styles in them.
This is a post that has taken months to complete, but addresses something that I don’t think I’ve seen sufficiently covered for anyone who is new to WPF. Resultantly, we’re going to go through it slowly and I’m officially begging for additional questions at the end.
Part of the problem with styles and templates in WPF stems from the fact that Blend allows a wonderfully simply way of creating a copy of a template:

It then gives you something that looks like this:
<Style x:Key=”My_Template” TargetType=”{x:Type Button}“>
<Setter Property=”Template“>
<Setter.Value>
<ControlTemplate TargetType=”{x:Type Button}“>
<!– blah blah blah –>
So, from a usability point of view… I told it to create a Template and it created a style. I judged from this that styles and templates were roughly the same thing.
And I was confused.
So, first, I’ll try to explain styles and templates by explaining how they work and then I’ll draw an analogy that I hope is helpful.
Let’s say you have a button.

You can change all sorts of properties of that button… visibility, background, width, height, margins, border thickness, alignment, font, whatever.
If you have a dozen buttons and you want them all to have the same properties, you can create a button style that specifies those properties and assigns them across the board. You can edit a style in Blend by selecting your control, clicking in the menu: “Objects -> Edit Style -> Edit a Copy…“.
Style editing in the objects tab will look like this.

As you can see, there are no objects in the visual tree to play with… only properties to assign in the properties tab.

When you assign a property in Blend, your styles will save that assignment as setters and values. Let’s say we wanted all of our buttons to have green 18 point font bold text. We could create a style that looked like this:
<Style x:Key=”GreenBorderButton” TargetType=”{x:Type Button}“>
<Setter Property=”Foreground” Value=”#FF00FF00” />
<Setter Property=”FontSize” Value=”18“ />
<Setter Property=”FontWeight” Value=”Bold” />
</Style>
The styles can only define properties that belong to the control type that they are styling (which is defined in the “TargetType“). Also, styles can only give information for properties the control already has and only in the way that the control is already set up. For example, because there is no property for changing the corner radius of a button, you can’t change the corner radius of a button using a button style.
However, what if we want to change something about the button that we can’t change with the given properties? For example, let’s say we wanted to see all the text show up twice.

In order to do this, we need to make what I’m going to call “structural changes” to our control. Structural changes are changes in the actual guts of the control, changes to the base elements that make up the control. For this we need a control template.
Boiled down to their essence, templates are little chunks of XAML that are inserted whenever you use your control. When you right click on something and go to “Edit Control Parts (Template) -> Edit a Copy…“, Blend takes the default XAML that makes up your control and places it in the resources so that you can change it at your whim.
You can get to the Control Template using the right-click method described at the top of this post. Your basic button template will look something like this:

<Style x:Key=”MyButtonStyle” TargetType=”{x:Type Button}“>
<Setter Property=”Template” Value=”{DynamicResource MyButtonTemplate}” />
</Style>
<ControlTemplate x:Key=”MyButtonTemplate” TargetType=”{x:Type Button}” >
<Microsoft_Windows_Themes:ButtonChrome x:Name=”Chrome” >
<ContentPresenter />
</Microsoft_Windows_Themes:ButtonChrome>
</ControlTemplate>
We can go in and add an additional ContentPresenter in here, like so:
<ControlTemplate x:Key=”MyButtonTemplate” TargetType=”{x:Type Button}” >
<Microsoft_Windows_Themes:ButtonChrome x:Name=”Chrome” >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height=”.5*” />
<RowDefinition Height=”.5*” />
<Grid.RowDefinitions>
<ContentPresenter Grid.Row=”0” />
<ContentPresenter Grid.Row=”1” />
</Grid>
</Microsoft_Windows_Themes:ButtonChrome>
</ControlTemplate>
And now our button shows all the content twice, one right on top of another.
The best way I’ve found to think about it is to think of your control as a car.
The dealer give the buyer a list of things that they can change about the car… interior color, leather or fabric seats, 4 or 6 cylinder engine… these are properties of the car… defined in the car “style”. (Basically, you can think of everything that you’re allowed to tweak at this website as the style of the car.)
<Style x:Name=”MySpecialCar” TargetType=”{x:Type Camry}” >
<Setter Property=”ExteriorColor” Value=”Blue” />
<Setter Property=”TransmissionType” Value=”5SpeedManual” />
</Style>

But let’s say that the buyer doesn’t want a normal seat… she wants a big comfy chair in place of the regular drivers seat. This is something outside of the scope of the list of things she was allowed to choose from, so they have to draw up new blueprints for making this new car. They have to create a new car “template”.
If our normal Camry blueprint looks like this:
<ControlTemplate x:Key=”MySpecialCarBlueprint” TargetType=”{x:Type Camry}“>
<CamryFrame x:Name=”CamryFrame” >
<Seat Type=”Drivers ” />
<Seat Type=”FrontPassenger” />
<Seat Type=”BackBench” />
</CamryFrame>
</ControlTemplate>
We can go in and replace :
<Seat Type=”Drivers ” />
With
<Seat Type=”ComfyChair” />

You may also notice that, with this model we could get rid of all the other seats except the drivers seat or we could add 12 new rows of seats. We can change anything about the car because we’re down into the original car blueprint.
This is the basic difference between styles and templates.
- A style is a list of properties that can be assigned in bulk to a control.
- A template goes a big step further and actually defines the underlying structure of the control.
You may be asking: “So how do these two work together? And what is this Data Template think I keep hearing about?”
Given that this post is getting dangerously long already, I’m going to address those issues in a couple more posts on styles and templates.
I’ll end on this note: if you are working in WPF and you’re having trouble with styles and templates, please read all of these posts (as I get to them) and ask questions in the comments section. I’m pretty good about getting to the comments questions and if the question is big enough, I’ll write a whole post on it. There are few things more vital to a WPF developer/designer than to have a firm grasp on styles and templates. It is in this understanding that the power of WPF really comes out.
- Who’s The Boss? Property Priority in Styles and Templates (coming soon)
- Create Conditional Styles and Templates (With the Magic of Triggers) (coming soon)
- So How Do Data Templates Fit Into All This? (coming soon)
The ComboBox is not the most complex of the WPF applications, but it can be a little tricky, so lets do a general overview post of it before we go into the specifics of how we’re going to make it work.
First of all, if you’re going to test your comboBox design, you should have it hooked up to an ItemsSource. Don’t have one? I have a tutorial in which I walk through attaching an RSS feed to your control. It was originally written for the ListView, but it will work fine for a ComboBox.
To start out… this is your standard ComboBox:

When working on a comboBox, you have a couple of options for the Items inside the ComboBox. If the options never change and are not data-driven, you can just toss come ComboBoxItems into it. Otherwise, you can connect it to some kind of ItemsSource (see the link above).
All of my examples are done with a data-driven ComboBoxes, but you should get the desired results if you run through the tutorials with ComboBoxItems.
First, a little bit about the structure of the comboBox.
Read the rest of this entry »
So, after months of delay I finally figured that there are probably some people out there who want to figure out how to make the WPF listview look the way they want it to look.
A quick note: I will be dealing almost entirely with the listview look. If you want the listview to do something (functionality) or look a certain way and you can’t find the answer here, leave a comment with your suggestion and I’ll try to blog about it and place a link to the answer in my listview FAQ.
My goal is to create a significant repository on getting the stinking listview to look the way you want and do what you want it to do.
You’re probably here because, compared with most of the WPF controls, Blend gives very little guidance on how to deal with the listview (even though you use the listview for practically everything you do). So we’re going to start with the basic structure of the listview. This is what the basic listview looks like in the XAML.
<ListView>
<ListView.View>
<GridView>
<GridViewColumn Header=”Column Header“/>
</GridView>
</ListView.View>
</ListView>
So this post will start out giving basic guidance on what to edit when you’re trying to edit the various parts of the listview. I will update these sections with links and tutorials on listview specific tasks as time goes on.
Read the rest of this entry »