Using an adorner with WPF DataGrid
Growing tired of manually keeping track of my work time, I wrote FlexTime, a small application that keeps track of the times I work on my computer and then summarize the result in a time sheet each week.
All good fun, but since I had a few moments to spare, I decided to add support for project information: textual information on what I have been working on. So I added a DataGrid and wrote a small plugin framework so that users can roll their own storage backend.
Again, good fun but then I wanted to indicate in each grid cell the plugin name of the particular comment. Just a small, quiet label at the bottom right, like the image below.
The obvious solution to use a DataGridTemplateColumn and a Grid with two text boxes has focus problems. Instead, a cleaner solution is to use an Adorner class on a DataGridTextColumn.
The basic idea is to attach the adorner layer to the data grid cell and provide a dependency property where we can set a data template for the adorner using XAML.
First the adorner class. We simply use a ContentPresenter, passing it the template and content.
Then we derive our own class from the DataGridTextColumn where we define a new dependency property for the template and wire up the adorner class.
And finally we can utilize the new adorner column in the XAML:
Both the power and complexity of WPF does not cease to amaze me. Pretty schweet, methinks and great fun too. Look out for the next release of FlexTime.