0

I have been working on an issue where I want a scrollviewer whose scroll bar is replaced with a button to scroll left and a button to scroll right. (seperate buttons in different grid locations), however I have been struggling to get my head around it as the scrollviewers quite a complicated control.

Lets say I am creating a new control template for scrollviewer and I want to add an extra button into the template that would scroll the content to the right. How would I hook into the command that moves the horizontalscrollbar right. For example my code might have the following scroll bar and buttons and I want to hook into "horizontalscrollbars" right scroll button.

                        <ScrollBar x:Name="HorizontalScrollBar"
                                    Grid.Column="0"
                                    IsTabStop="False"
                                    Maximum="{TemplateBinding ScrollableWidth}"
                                    Margin="0,0,0,0"
                                    Minimum="0"
                                    Orientation="Horizontal"
                                    Grid.Row="0"
                                    Visibility="Collapsed"
                                    Value="{TemplateBinding HorizontalOffset}"
                                    ViewportSize="{TemplateBinding ViewportWidth}"
                                    />

                        <Button Grid.Column="0"
                                x:Name="LeftBtnScroll">                    
                        </Button>
                        <Button Grid.Column="2"
                                x:Name="RightBtnScroll">
                        </Button>

p.s I don't have access to blend.

1 Answer 1

1

You probably won't like this answer, but you'll also have to define a ControlTemplate for the Scrollbar element as well... that is the element that has the Buttons that move the content. You can find the default ControlTemplate for the Scrollbar on the ScrollBar Styles and Templates page on MSDN.

On that page, you'll see a section called ScrollBar Parts. This details the named parts of this ControlTemplate. A named part is an element that the 'code behind' accesses to provide some functionality. When defining a new ControlTemplate, you must include these named parts, or some (or maybe all) of the functionality will not work as expected.

The Buttons in the ScrollBar are the named parts that are of type RepeatButton. However, using this method, you will only be able to define your own version of those RepeatButtons and not add additional ones. The functionality that actually moves the content comes from the IScrollInfo interface and have not been exposed by the ScrollBar control, so you cannot hook additional Button clicks to them.

Failing that, the alternative is even longer... you'll need to define your own custom scroll panel that implements the IScrollInfo interface. In doing this, you can then provide your own custom scroll functionality and arrange everything exactly the way that you want:

<ScrollViewer CanContentScroll=”True”>
    <YourXmlNamespacePrefix:YourCustomPanel>
        <!--Your Content-->
    </YourXmlNamespacePrefix:YourCustomPanel>
</ScrollViewer>

This operation is explained beautifully in chapter 7 of the WPF Control Development Unleashed book... you can find a PDF copy online at http://www.adorkable.us/books/wpf_control_development.pdf. It's well worth a look.

[UPDATE 27/11/2014: Sorry, this link is now broken... you'll have to buy the book to see it now.]

2
  • Thanks, Ill probably go down the route of defining a scrollbar element. I'm wondering how I am going to separate the two buttons though.. I presume I would have to make two separate templates for the scroll bar for each respective button... Commented Nov 22, 2013 at 14:58
  • Yes, if you look at the first linked page, you'll see a number of ControlTemplates that have been defined for the various parts of the ScrollBar.
    – Sheridan
    Commented Nov 22, 2013 at 14:59

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.