Generate All The Things

or how to save yourself some work in the long run

Coding is a lot of work, and sometimes it gets repetitive too. I don’t like repetitive work so most of my projects grow some kind of code generator. Usually it starts with a unit test that outputs a simple class definition, and before you know it, it generates a complete data-access layer with NHibernate mappings in FluentNHibernate that generate SQL create scripts… but that’s a story for another post. So, here’s my default ‘plan’ for building ad-hoc code generators: Create a test project Add a InternalsVisibleTo attribute to the target project with the assembly name of the test project, so that the test project can access methods and properties defined as internal in the target project. [Read More]

Cancel That Please

How to make the user do what you need him to do

I was working with the TabControl the other day and I wanted to prevent the user from abandoning the task he was currently performing by cancelling the selection of a new tab. This was more difficult than expected because Selector does not implement a SelectionChanging event with which you can cancel the selection changes. Enter the SelectorAttachedProperties.HasActivatableSupport dependency property. Together with an IActivatable interface that is implemented by the model for the tab page it will take care of the nasty details of cancelling a selection. [Read More]

WPF Magic Without Frameworks

How to build simple user interfaces without big frameworks

Building WPF applications is challenging is you’re new to WPF. There are lots of frameworks, components and toolkits to choose from and WPF is no small framework itself. I’ll show you some tricks to build a WPF UI without toolkits but with magic. But first some basic concepts I’ll use a lot. A View is WPF FrameworkElement or a control derived from it. A ViewModel will be (in) the DataContext of the view and contain everything the view needs to do its viewy things. [Read More]