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.

<RadioButton>

<RadioButton.ContentTemplate>

<Rectangle … />

</RadioButton.ContentTemplate>

</RadioButton>

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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: