September 2008 - Posts
My last post on WPF and Skinning seems to be a spam magnet. The comments on that post are like 600% higher than normal but they are all spam.... sheesh
Last night I presented for the Mankato .NET User Group on Windows Presentation Framework and a question came up about skinning. Programmatically changing styles in a way similar ASP.NET themes. This piqued my interest so I thought I would give it a try. My first attempt was to create a sample app that defined styles in C# and set by button handlers:
<Window x:Class="Themeing.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal">
<Button Click="Button_Click">Style 1</Button>
<Button Click="Button_Click_1">Style 2</Button>
<Button Click="Button_Click_2">Clear</Button>
</StackPanel>
<TextBlock Name="myTextBlock" Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Center">Sample Text</TextBlock>
</Grid>
</Window>
The codebehind looks like this:
public partial class Window1 : Window
{
Style style1 = new Style(typeof(TextBlock));
Style style2 = new Style(typeof(TextBlock));
public Window1()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Window1_Loaded);
}
void Window1_Loaded(object sender, RoutedEventArgs e)
{
style1.Setters.Add(new Setter(ForegroundProperty, Brushes.Green));
style1.Setters.Add(new Setter(FontWeightProperty, FontWeights.Bold));
style2.Setters.Add(new Setter(ForegroundProperty, Brushes.Purple));
style2.Setters.Add(new Setter(FontSizeProperty, 25.0));
}
private void Button_Click(object sender, RoutedEventArgs e)
{
myTextBlock.Style = style1;
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
myTextBlock.Style = style2;
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
myTextBlock.Style = null;
}
}
We create two styles (style1 and style2) which for the sake of this example target TextBlock. We then populated our styles. One style is Green and Bold and the other is Purple and pretty big. You will also notice we have three Button_Click handlers (I really didn’t spend the time with naming). The first handler sets myTextBlock’s style to style1 and the second sets it to style2. The third sets the style to null which is to return to no styling at all.
Once our little app loads we get this:
Style 1:
Style 2:
and finally Clear:
As you can see its pretty simple to set styles in codebehind but creating styles in C# is kind of a pain. In part 2 I’m going to try and change styles that are defined in XAML rather than in C#
I’ve been spending some of my free time on my Product Key Manager project and finally posted some screenshots on CodePlex. The next version will have quite a few changes affecting the UI, XML handling, and data access. I’m cleaning up the UI and attempting to match the host OS a little more. The old black and orange just felt too dark and out of place and wasn’t coded well. There was redundant XAML all over the place just defining the gradient that defined the black background. I’m also taking elements of the UI to the next level and making it much more interesting that just a list box.
I’ve been working on all of the C# code, from code behind to data layer and trying to make it better. I’ve also been running code analysis tools to improve the quality and performance of the code. This change also brings in .NET 3.5 SP1 and SQL Server Compact Edition (SSCE) 3.5 SP1 which will allow PKM to run with the ANYCPU flag rather than being forced to use the x86 flag which forces it to run in the 32-bit emulator on 64-bit Windows. Sure 64-bit gives us almost no advantage for this app, but that’s just the way I roll.
The major changes include a much lighter colored look, and moving from a ListBox style UI to one that is more like Explorer (WrapPanel is really cool!). The color change is more about making cleaning up the UI and using styles. Once I’m done the coloring will be defined once so it can be more easily changed. I’m also going to try and make it look more like the host OS more if possible.
This shows that the search similarity stuff is still there.
There are two new buttons on the Edit Dialog that allow you to set the icon and add notes to the product. Neither of these have been fully fleshed out yet so no screenshots yet.
I ran across a problem that took me nearly a week to solve in my Product Key Manager project. I was using some Vista icons from the Visual Studio 2008 Image library and was having really weird problems where some, but not all, icons would show up the first time but not a second time. I eventually stumbled upon this thread: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/bebf27e5-393b-4a8b-bd79-252215ebe095/.
This isn’t something limited to WPF, it affects WinForms as well, but Vista supports PNG icons but .NET does not yet support it. I had to open the icons in the Visual Studio icon editor and delete the 256x256 PNG icon, which isn’t a big deal because I wasn’t using them in that size but it would be nice if .NET 3.5 SP1 which came out after Vista included support for Vista icons.
So if you run into weird problems with icons in .NET make sure you don’t have an PNG format icons in the icon.
I bought a copy of Programming Windows Presentation Foundation by Chris Sells and Ian Griffiths around the time it first came out while WPF was still in Beta more than two years ago. I started reading it and at some point I got distracted and stopped reading it. I literally forgot I had it for two whole years as it was sitting on my bookshelf. I'm guessing I got busy and took a break from reading it and ended up with Petzold's book and Nathan's RTM based WPF books and just relegated it to the beta book section (In fact it was sitting next to my codename Indigo book) without realizing that it was the Sells/Griffiths book.
After some WPF based conversations at the Mankato .NET User Group and agreeing to do a WPF presentation I took a second look at it and was pleasantly surprised to discover which WPF book it was. I started re-reading and I'm disappointed I never found time to finish it earlier. It is in my opinion one of the best books, especially for people wanting to dive in to WPF. I really do like it that much.
It is a beta book though and I did notice some things that hand changed since the book was published, heck it even said AKA Avalon on the cover. I wondered if there was a second edition of the book that had been updated since RTM so while at the bookstore tonight I decided to take a look and boy is there. The second edition is about twice as thick as the first edition! Including the index the first edition comes to a mere 430 pages while the second is a heavy, by contrast, 834 pages with index.
There are a lot of great new chapters on Input (Mouse, Keyboard, Ink and Commands), a dedicated chapter on NavigationWindow and XBAP applications, Text and Flow documents, Printing/XPS, 3D, and more. There are also a couple new appendices including one on Silverlight. I would definitely say that this is a must have book for WPF developers and a great starter book for new WPF developers.
I was messing around with the Zune software today and stumbled upon a really cool view while listening to some music.

Using cover art for the background is pretty sweet. I kind of wish that I could have this type of thing as the background on all screens not just the playing screen.