One of the most interesting concepts in WPF is that a control's logic is completely decoupled from its layout: a button is something that reacts when user clicks it raising a Click event, despite of how it looks like. If you think about it, it's definitely a breaking change compared to Windows Forms, and it allows designers to completely reskin UIs, while keeping the behavior intact.
When you create a new custom control, Visual Studio adds two files to your solution:
- a C# (or VB.NET) file, which is supposed to hold all your control behavior logic
- a Generic.xaml file, which provides a default control template for its layout.
The Generic.xaml file contains various child UI elements that need to interact with each other and with the underlying behavior logic, and this has to be done without introducing dependencies from the logic to the UI. In my experience building custom WPF control, I've been able to achieve that following three main rules:
- Triggers (and eventually Binding between UI elements) to encapsulate layout-only logic within XAML
- Handle data flows (both One-Way and Two-Way) from UI to control logic with Binding
- React to UI's events using Commands
While the first two points are pretty straightforward, the third deserves a small overview. Let's suppose we're building an "File selector" custom control, similar to the following:
The behavior needs to be notified when the user clicks the Browse button to display the standard Windows Open File Dialog. Obviously we could assign the button a unique name
and attach its click event in C# code:
That kind of implementation has a couple of serious drawbacks:
- It works until there's a button named browseButton somewhere in the control template. If a designer reskins our control and doesn't provide a button with that exact name, or if he wants to use some other event, or even a different control type that doesn't inherit from button, our logic stops working correctly;
- Our logic is so tightly bounded to the UI that we can have one and only one control triggering the opening of our open file dialog.
WPF's Commands infrastructure offer a much more elegant (and simple) solution to all those issues. A Command is the object oriented alter-ego of a functional logic, that can be enabled or disabled, triggered by an input gesture and associated to some code, regardless of what and how many UI elements are bound to it.
So, our first step is to create a proper command, stored in a static readonly field initialized into the control's static constructor. A command is a class that implements the ICommand interface; WPF has several built-in commands for the most common tasks (Cut, Copy, Past, and so on... here there's an exhaustive list). In our case, we can use the already existing RoutedCommand class:
Two main details to notice:
- After building the BrowseFileCommand, you need to bind its Executed event to a proper event handler and register that binding to the CommandManager;
- The event handler is static, but you can retrieve a reference to the current BrowseFile instance looking at the e.Source property;
- There's absolutely no reference to any UI element in the code we've just written.
What about the general.xaml file? All we need to do is associating the BrowseFileCommand to the button object with its Command property:
Now, our BrowseFile behavior is well encapsulated within the C# class and its BrowseFileCommand custom command; a designer who wants to completely re-invent our control's layout only needs to attach it to the UI elements he prefers; Notice also how easy was to include a context menu with the same behavior, simply reusing the same command.
In a next post, I'll show you how to build a completly skinnable and bindable custom control from scratch.
Cheers.
