Slider will raise its ValueChanged event during initialization while counter is still null.

This is an example of a larger problem that I've been running into using WPF, that UI events can fire at any time, and that there is no single place where I can put my initialization code so that it's guaranteed to run after all the pointers owned by the WPF system have been initialized but before any UI events have fired.

What is the most elegant way to deal with this? The fact that this specific example should use data binding is beside the point.

The true answer to this question is to use the MVVM pattern where window code behind files contain little to no initialization code.

In this pattern, the UI is connected to the rest of the code with data binding only. You write special view-model classes that implement INotifyPropertyChanged and take your business logic and expose it as a series of properties that UI binds to.