C# – How to add scrollbars to your custom control in WPF

cnetscrollbarswpf

After asking this question I realized that perhaps I should have asked a more generic question. So, here goes:

In WPF, I'm creating a custom Panel-derived control. To that control I would like to add horizontal and vertical scrollbars and control them myself (get & set min/max/value/pagesize). How can I do this?

This is my first encounter with WPF, and I'm not yet familiar with Templates and Styles, so I don't know if answer lies there or somewhere else.

Best Answer

OK, I found it! Three easy steps:

  1. Implement System.Windows.Controls.Primitives.IScrollInfo on your custom control;
  2. Add your custom control to a ScrollViewer;
  3. Set the CanContentScroll property on the ScrollViewer to True.

Voila!