Tag Archives: WPF

Binding a WPF Grid Column Header

This is another of the occasional posts that are primarily here to remind me how I did something, and because it might be useful to somebody else!

Over recent years all of the desktop based windows applications I’ve developed have had user interfaces created using Windows Presentation Framework, having done Windows Forms extensively previously, it’s  a definite step up, with great support for data binding user interface elements to data fields. As such it is pretty straightforward to produce user interfaces with a really clear separation of concerns, using a Model-View-ViewModel design.

A recent change request for one of my projects has been to provide dynamic labelling for data fields – different sorts of users store the same data in the same fields, but use different terminology which needs to be reflected in the labelling. Whilst the WPF binding support is able to cope with labels without an issue, I hit a bit of a dead end with a large data grid that was used in the application where the column headers were required to change. Whilst the data items in the grid can be bound without problems, the column headers just wouldn’t work.

Having failed with the standard binding, I then looked at using FindAncestor to try and point the column header at the ViewModel that was mapped to the data context of the window, again this didn’t work.

After much digging around and experimenting, I eventually came upon this question on StackOverflow which while it was about binding column visibility, demonstrated the same basic problem, that you couldn’t bind column properties. The first answer by Cameron MacFarland explains why it doesn’t work, and gives a good answer that solved the problem for me. It’s not the first question that comes up when searching for the problem on Stack Overflow, but the solution offered by Cameron is a much neater and cleaner solution than many of the other solutions offered to the similar questions.

The issue is that under WPF data grid columns are not part of either the visual or logical tree, and therefore the usual relative binding mechanisms don’t work. The solution is to use a static binding, and create a proxy class in the application code that provides what would come via the relative binding.

The property is set up as follows:


    


And then you need to create this binding proxy class:

public class BindingProxy : Freezable
{
    protected override Freezable CreateInstanceCore()
    {
        return new BindingProxy();
    }

    public object Data
    {
        get { return (object)GetValue(DataProperty); }
        set { SetValue(DataProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Data.
    // This enables animation, styling, binding, etc...
    public static readonly DependencyProperty DataProperty =
        DependencyProperty.Register("Data", typeof(object), 
        typeof(BindingProxy), new UIPropertyMetadata(null));
}

Although the example Cameron gives is for column visibility, once the binding proxy is set up it works just as well for other column properties such as the column header I was trying to bind.

Developer Day Number Six

Today was the sixth of the Developer Developer Developer events at the Microsoft Campus in Reading, and as with the previous events, I spent the day enjoying the sessions. As in previous events what I actually attended didn’t quite match up with what I thought I would attend, but the beauty of these days is you can quite easily switch dependant on what takes your fancy on the day, or indeed which sessions have seats!

First off I sampled the first part of Oliver Sturm’s double presentation on producing business applications with Windows Presentation Foundation (WPF). Like most people, what I remember about WPF is the flashy eye candy filled demo applications – what Oliver aimed to do was show that alongside the eye candy was a strong platform that could produce the kind of ‘boring’ business applications that most people end up writing, something in which he very much succeeded.

I commented after DDD4 that the kind of material that Oliver covered needed an early slot, rather than the graveyard slot where everybody was tired, and it is great to see that the comment had been taken on board, as a result I thought I gained a lot from attending both the first and second parts of the presentation (although it would have been better in successive slots I have to say), and certainly have a better idea of what I can do with the new framework.

Another session that gave me a better idea of what I can do was the slightly mis-titled Cruise Control .Net session presented by Paul Lockwood. I say mis-titled as the titular piece of software only appeared at the end of the session, much of the earlier part was looking at all the other bits of software that Cruise Control .Net actually uses, and which provide much of the power of the continuous integration process. No matter as it was an interesting session, and certainly gave me some pointers towards what is needed in setting up a continuous build process. Having said that, at work we’ve been saying we’re going to set up a continuous build process for a long while, whether we’ll actually get round to doing it is another matter!

After that it was back to see Oliver for part two of his session, and then from there on to lunch.

The main lunchtime activity was the Grok talks, of which more in a moment, but first the one thing that really annoyed me about the day (well aside from the car service indicator, but that’s another story) which was the way lunch was handled. Now they’ve tried various ways around this from full scale hot food, through to the bagged lunches they have now. The bagged lunches seemed to have worked fairly well, but it is always slightly slow because they randomly pack the bags so whether you’re just like me and fussy, or more importantly have food allergies you sometimes need to look through to find a reasonable combination. Now obviously there have been comments about this, as this time they had taped all the bags shut and the only option was vegetarian or meat – but still with the same random selection. There were also Microsoft Events staff posted at each table handing the bags out and being downright rude if you tried to have a look at what was in the bags. Heaven help you if you actually had a legitimate reason to be careful what you got. Luckily my random selection was pretty good and I didn’t get a sandwich ruined by tomato and cucumber, and even struck lucky with the flavour of crisps. But seriously taping the bags shut may remove a symptom of the problem, but it’s not actually solving the problem, it’s just annoying!

Anyway, onto the lunchtime talks. In an improvement from last time they were actually held in Memphis rather than in the foyer. There was still a bit of a problem with noise as the doors were open and people were chatting outside – not helped by the lack of a microphone for the speakers in that room, but it was a definite improvement. Whilst on the subject of microphones, in answer to the organiser, who shall remain nameless, who introduced and closed the day in Chicago by saying both times “you can hear me, I don’t need a microphoneâ€?, “we can’t, and you doâ€?! I know it’s a pain to use a microphone, (and I know other people who don’t like it and think they can get away without) and it’s probably not comfortable, but you can’t be heard at the back if you don’t.

Anyway, back to the Grok Talks – there was a good mix of topics including tips on packaging up your custom controls, a demo of Windows Power Shell and some tips on how to speed up Reflection. Probably the two most memorable were firstly a senior programmer, whose name unfortunately I don’t remember, who did a primarily non-technical presentation about a recent project he led implementing a patient record system in the UAE. Basically by reviewing the project from a business perspective it highlighted all sorts of gotcha’s for other people developing software in foreign countries. In terms of software design, things like other countries having names that don’t fit neatly into the forename/surname structure used here are important, also the d’oh moment when they realised that having a picture of the patient was pretty useless when large numbers of the women wore a burqa was good to share. He also highlighted that the scheduling aspect of the system was complicated by Ramadan as the scheduling algorithm would be different in that period. He also highlighted issues of staff morale, and just getting things done – all useful stuff that some might consider common sense, but are easy to miss on a complex project.

The second most memorable was for totally different reasons. This one was Guy Smith-Ferrier talking about Extension Methods. It was memorable not because of the topic, but because Guy chose to do it as a Pecha Kucha where the presentation is limited to twenty slides, each shown for exactly twenty seconds. After those twenty seconds the slides automatically move on, whether the speaker is finished speaking to the slide or not! Even if you’re not really massively enthusiastic about the subject, the format itself does bring in a strong element of interest as you watch to see if the speaker succeeds or doesn’t manage to keep up. Although there were a few points were Guy fell behind, and even one occasion where he was waiting for the slide to move on, he largely succeeded in coping – maybe an idea to try for more speakers next time?

After lunch I stayed put in Memphis for a Question Time style session on recruitment, not because I was massively interested in the subject, but because the panel included Barry Dorrans on the panel alongside a recruitment consultant. To understand why, have a read of some of the posts on his blog… Anyway, it was a worthwhile session, as there was a good discussion of the pros and cons of going freelance – something I’ve considered before, but rejected – which was an eye opener, particularly the comments from the recruitment consultant about the issues with trying to swap back again. I also felt somewhat better about the lousy pass rate we got on the programming test we gave to potential developers on our most recent recruitment round – the manager on the panel said only one in twenty programmers pass his simple test which sounds much the same as ours. I also came away with a great little test for helpdesk operators too which I guess I’ll have to pass on. As to Barry he was entertaining and animated as always, and managed to not lay in to the recruitment consultant too much – and when he did, about the lack of technical knowledge they have, he largely agreed!

The final session was perhaps the one I had least idea before hand which I was going to attend. Eventually I resisted the temptation of Swaggily Fortunes, and went along to hear James Winters talk about how to write a Facebook application, mainly out of curiosity.

The first thing I learned from the session is that in order to make money from writing for Facebook you don’t need to do anything complicated, indeed James showed us an application that recently sold for about $25,000 that in reality took about three hours to write. To understand why, you have to go back and look at how the Facebook model actually makes money – advertising. Therefore the more users an application has, the more it is worth – so all the stupid little applications that some of the people wanting to use Facebook as a business tool tend to look down on are actually worth significantly more because they generally have many more users than the more serious applications.

The general impression I took away from the session is that a basic Facebook application is actually relatively simple to produce – the real skill is coming up with an idea that has the sort of viral penetration to spread through thousands of users, which is how you can make any sort of money as a Facebook developer. Aside from that the applications are really just web applications, albeit with some functional limitations imposed by Facebook.

Anyway, all in all it was a good day, and I picked up lots of useful bits and pieces – and maybe if I can think up a good idea I’ll make my millions writing a Facebook application… maybe not. Oh and if you’re wondering why I wasn’t micro-blogging along with some of the others on Twitter, blame the Twitter mobile service, as I tried to hook up but it wasn’t until I got home that I realised I wasn’t following the feed, so nothing had worked. Maybe next time…

DDD6 092 and DDD6 125 originally uploaded by blowdart2000.