WPF still doesn’t provide out of the box common Font Chooser dialog, and I can’t realize if Microsoft is going to provide such the dialog in the forthcoming WPF 4.0 version.
Currently we have the following alternatives:
- Microsoft Windows SDK provides the WPFNotepad sample where the Sample FontChooser is implemented. This implementation conforms to recommendations in the WPF Font Selection Model whitepaper.
- Sample Font Chooser by Niklas Borson.
- Pure XAML Font Chooser by Norris Cheng.
The former two items are full-fledged Font Chooser dialog implementations, and the latter one is the sample of how to implement the basic Font Chooser in pure XAML. Maybe there are other implementations but I could not find them.
From full-fledged options I prefer the one of Niklas Borson: it has more compact design and, as much as I tested it, all its features work fine. The only drawback is that the dialog initialization takes too much time.
There are things I don’t like in both the first and the second implementations: they are written rather in the Windows Forms style than in the WPF one. The dialog controls and layout are defined in the XAML. All the logic is implemented in the dialog code behind file, and this file is long and complicated. There is no one Binding object used!
I have rewrote the Font Chooser dialog basing on the approach close to the MVC pattern: there is the Font properties class, the Model, and the dialog controls binds to the properties of this model class in XAML. The code behind file contains as little code as possible. The implementation consists of the following main parts:
- FontInfo class contains the set of properties describing the Font in the WPF world. FontInfo class plays the role of the Model in the MVC pattern.
- The dialog contains a lot of controls defined in the ChooseFont.xaml which represent the View. These controls are bound to the Font properties defined in the FontInfo class.
- ChooseFont.xaml.cs file code provides the constructors, OK button handler and some event handlers implementing the auto complete behavior in the dialog.
You can download the dialog source code here:
Oleg V. Polikarpotchkin