views:

424

answers:

2

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.

+1  A: 

Yes. The answer lies not in the Panel but in the ScrollViewer. Your panel shouldn't take care about scrollbars. Let ScrollViewer measure and arrange your panel. So your visual tree will include ScrollViewer first, then your panel:

    <ScrollViewer> 
      <cc:YourPanel/>
    </ScrollViewer>

If you want to control ScrollViewer, you will probably want to either inherit from it or customize its template.

Anvaka
The problem is - my control may **NOT** change its size. ScrollViewer is based on checking the size of the child control.
Vilx-
+1  A: 

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!

Vilx-