Powered by Twitter Tools.

March 2009
M T W T F S S
« Jan   Apr »
 1
2345678
9101112131415
16171819202122
23242526272829
3031  
Chris Donnan

Create Your Badge

Chris Donnan : Programming – Brooklyn Style

software, trading, family, fun

WPF Attached Behavior – resize a window when focusing on a tab

I have been using the ‘attached behaviors’ pattern more and more in my adoption of WPF. In general, I spurn significant ‘code behind’ in my windows/ controls, etc – unless absolutely necessary. Using attached properties in WPF, you can actually build up a library of behaviors that you can attach to various objects in your application. I have a large # of these behaviors in my current project – especially for Xceed’s WPF DataGridControl.

I will use this simple example to illustrate the concepts.

The goal is to allow a window to resize when a specific tab is focused. I have a window with a number of tabs, and for most tabs it is appropriate to have the same size window (relatively small). There is one tab however that is too squashed to look at in this small-ish size. What I decided to do was to save the users from manually resizing the window each time they went to it. The result was this simple ‘attached behavior’.

Attached properties in WPF are properties that you can attach arbitrarily to any dependency object. In this example, I simply attached my TabBehavior to the TabItem like this in XAML:

<TabItem  Behaviors:TabBehaviors.ResizeOnFocus=”805″ … />

The underlined bit is my attached property. It is simply giving the size I want to resize the parent window to when this tab gets focus.

So – how does this work? Well the effect of adding an attached property is to pass the value in the argument + the object the attached property is decorated on… So you get a call with the TabItem and the 805 value passed into your code.

The 1st step is to declare a public static class called TabBehaviors, next we declare our 1st attached property; ResizeOnFocus – like we see above:

This is very much like a normal Dependecy

Property in WPF. Note the last bit – the callback OnResizeFocusChanged; this is what we get when the property is set on an object.

The OnResizeFocusChanged method is where the magic starts (I have renamed that method after I took my screenshots ;) ). The 1st argument passed in is the object the attached property is attached to – the TabItem in our example. The 2nd argument has the NewValue – the 805 value in our current example. The next bit – the DependencyPropertyDescriptorFromProperty is a way to add a ‘changed’ handler to the IsSelected property of the tab item. This is sort of like using a .net event, but it is just the WPF Dependency Property way to do so. 
The next step is the IsSelectedChanged callback. This method will be called when the TabItem’s IsSelected property actually changes. When the tab is selected/ deselected is when we want to act.
This method uses a helper function to get the tab’s parent window. If the TabItem is selected it is going to get the current window width and store it in another attached property on the TabItem, then set the window to the width we asked in our 1st attached property. If it is now de-selected, we get that original size property and set our window back to it’s original size.
This is all done via the magic of attached properties. Here is the OriginalSize attached property:
Note that THIS is THE pattern for ALL general attached properties. You register the attached, you then provide get/ sets for it. You can see in the gets/ sets that it is sticking the property on the dependency object/ getting it from the dependency object.
Dependency Objects are essentially big ole’ maps and the metadata system is a schema/ type system for managing the values stuck in the Dependnecy Objects internal maps. This ‘mapness’ allows these attached properties to simply be stuck onto your objects even though the object never knows anything about your attached values. Using these attached values and methods called when attached, you can implement attached behaviors.
-There you have it-
Chris

Responses are currently closed, but you can trackback from your own site.



Semi-Random thoughts

I have talked with people for some time about work I had done in the past with copulas… today saw a good ref on Slashdot:

The Formula That Killed Wall Street

I do not like PowerShell. 

I still love Rake for building/ running .net apps

Visual Studio crashes all the time when editing XAML (it hates custom attached properties I think)

Xceed’s latest release of their grid is replete with sucky bugs

I feel overly awake at 11:30, that is what happens when you go to the gym at night

The global economy sucks, everyone is feeling it. I hope the low is not so low we can’t all bear it. I really do not want my wife and kids to be truly effected by it.

I love my iPhone and my MacBook Air

I am pissed that my skateboard went missing during our move :(

I love living in London

I have been thinking a lot about WPF, what are good practices for larger apps wrt managing XAML/ templates/ styles etc

I want a good lock free (or at least low, low lock) hashtable for C# (not a bad place to start Julian M Bucknall)

Windbg rock

The latest Ants profiler is pretty impressive, well done

OK, that is enough


Responses are currently closed, but you can trackback from your own site.