Today I began using the first version of the WPF data grid available on CodePlex. So far everything is making sense but I got caught out by an unexpected behaviour with one of the new C# 3.0 features, object initializers. The problem arose when I was trying to create columns in the data grid programmatically:
dataGrid.Columns.Add(new DataGridTextColumn
{
Header = "First",
Binding = new Binding("Length")
});
Despite my insistence that the computer should accept this without complaint it threw up the following error:
A TwoWay or OneWayToSource binding cannot work on the read-only property 'Length' of type 'System.String[]'.
OK so it doesn't like binding to a read only property. I found a few forum posts that indicated setting IsReadOnly on the column would cause it to become a one way binding which is what I really wanted:
dataGrid.Columns.Add(new DataGridTextColumn
{
Header = "Length",
Binding = new Binding("Length"),
IsReadOnly = true
});
This had no effect whatsoever. A little confused, I went for a walk to puzzle over what I was doing and when I came back I made one simple change that solved the problem:
dataGrid.Columns.Add(new DataGridTextColumn
{
Header = "Length",
IsReadOnly = true,
Binding = new Binding("Length")
});
So there are two things I learnt today.
First, the properties you set in object initializers are set just like normal properties, in the order that you specify. In general I would say that if you are making properties where the ordering is important, you might want to rethink your API if possible. This little detail seems obvious now that I know it.
Second, WPF data binding is very eager and will try to apply any changes you make right away. This is a big difference from ASP.Net data binding which is a very lazy and badly behaved beast. If you want to mess with the setup of a WPF control in code, it might be easiest to do it before you bind any data to it.
17/11/2008 10:25 PM (UTC -08:00)
In this post I explain why FlashGot combined with Free Download Manager is a great combination and try to use it as an example of good object oriented design.
Downloading large files from web browsers has never been much fun. None of the major browsers has a download tool that really works for me so instead I pass files over to a little application called Free Download Manager which deals with resuming, acceleration, scheduling and so on.
Recently I wanted to save some web casts to disk so that I wouldn't have to open my web browser and stream them down each time I wanted to to watch them. The web casts in question were delivered in the same way that YouTube videos are, an embedded Flash player which links to another video file, usually a .FLV or a .MP4.
Firefox
The main reason I'm a Firefox user is the impressive add on community built up around it. Initially I looked at an add on with a promising name and a large number of positive reviews, Video Download Helper. When it detects a video being downloaded it highlights a button in your toolbar which lets you download the video as a normal file, using the standard Firefox download tool.
This works fine but leaves me a little frustrated as I have already installed another program to get away from the standard download tool. I put up with this for a little while and eventually figured out that once I had the download going in Firefox, I could right click it to copy the link, add it manually to the Free Download Manager and then go and cancel and clear the Firefox version. It works but it's way to many manual clicks for such a simple task.
A Better Plug In
The Mozilla site has done a great job of gathering all their extensions into a single place and giving you different ways to navigate them. Having a quick poke around I found myself looking at the most popular download management add-ons. Right at the top of the list is a little piece of magic, FlashGot. Like Video Download Helper this one gives you a simple icon to click when there is a video available for download, but the reason it really stands out is this little menu:
See that there? FlashGot automatically detects downloads managers I have installed and gives me the option to pass any URLs to them to deal with! Downloading now becomes a single click process, nice!
While writing this it occurred to me that this is a great demonstration of software design principles but on a larger scale than I normally talk about:
- Firefox has been built to be open for extension, without any knowledge of what those extensions might be doing.
- Free Download Manager is a well defined component with a single responsibility, take a URL and download it as fast as possible. It doesn't care where the URL came from or why.
- FlashGot provides a simple bridge between the two and makes the system as a whole much more valuable to me.
This could be used as an example of several different design patterns, anyone care to have a go at naming one?
17/11/2008 03:19 PM (UTC -08:00)