Chris Donnan : Programming – Brooklyn Style
software, trading, family, fun
Posted c#, wpf on Saturday, March 14th, 2009.
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.
Responses are currently closed, but you can trackback from your own site.
Posted programming on Tuesday, March 3rd, 2009.
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.



