Archive | September, 2008

Thinking in WPF

30 Sep

When first starting to develop in WPF most users will come from a background such as winforms where it is common practice to use an MVP approach to UI’s where a UI is “dumb” and is modified by controlling underlying logic. Controls were often chosen by how they looked as modifiying their look usually required overriding. Often code was just left in the code behind of the UI component which is even worse.

The WPF way of thinking tends to be that the visual appearance of a control doesn’t really matter as it can be modifed easily in XAML with a contentTemplate. Take the following pseudo code example which demonstrates how to make a radio button look like a normal button.



<Rectangle … />



Which you might then ask what indicates the control that should be chosen for a task. The answer lies in the underlying datatype of that control and the behavior that control provides. For example a game canvas can be made by using a listbox, as it contains a representation of a list of visual game items. For an example of this see the quake example linked to in the first post. Another example of this way of thinking presented itself to me the other day when I made 3 toggle buttons. After placing them I realised that I wanted to only have one button selected at a time. To do this I chose to instead use Radio buttons. Radio buttons are able to belong to a group which allows only 1 of them to be selected or toggled at a time. I then used a content from someones blog post about re-themeing a button control and just used that same template over my radio buttons.


First Post – Learning WPF

20 Sep

I have finally decided to make a blog because of all of the interesting things I am learning with .NET 3.5 and WPF.

Learning the WPF way of doing things can be quite challenging as you need to drop the poisonous mindset of Winforms to not fall into WPF anti-patterns. I recommend the following links and tips to learn WPF. Follow these and you will learn WPF quicker that I did. I must confess that i fought the idea of these concepts initially but I couldn’t achieve a reasonable design in terms of architecture with out them. Life is miles easier having taken the WPF plunge. This tutorial shows how the underlying behavior of a control is more important than how the control renders items. It also shows datatemplates which remove the need to extend controls to change the appearance. It is important to consider that listbox provides selectable item behavior and it binds to a list collection. How it displays data is irrelevant as it can be changed. (This tutorial shows how the traditional MVP is better replaced by a new model. The first links code sample generally follows this design pattern.

I should also point out that I have seen many people try to extend WPF controls. This is not particularly a great idea as you don’t seem to be able to extend multiple levels of inheritance. What you are better to do is to databind your Xaml control to an BaseClass such as Quake (see the first link for example). Then you can extend Quake, by making a class BigQuake. If you add BigQuake to the list that is the datacontext of the listbox, you can use a complex object model with many levels of inheritance without extending any XAML classes. This seems to be a common issue bought up by people first learning XAML because the mindset tends to be to extend a control to change its behavior. This should be a last resort in WPF.

Resource dictionaries can be used to provide centralized themes for your application controls.

A common usecase for businesses to migrate to WPF is the concept of streaming User Interfaces. This is due to the fact that XAML is an XML syntax so it is easily serializable. If you wish to do this, you really must attempt to embrase true WPF thinking otherwise the task will not be simple.

DependencyProperties seem like a nightmare when first learning WPF, there is no Textbox1.Text anymore. Its Textbox1.SetValue(TextBox.TextProperty, “text goes here”); . It is important to learn depenedency properties. A good link to do this is:
Another point of interest for people migrating to WPF from Winforms is that you should endevour to put as much as you can into XAML. Almost anything in the codebehind of a WPF Usercontrol tends to be due to laziness of not learning the proper XAML to do it. I do concede however that it is not always possible to avoid using code behind. I am for example at a loss as to how to bind my code to zoom to the current Mouse position with out resorting to code.