2

For an application that I am working on I want to create a custom control that has several buttons on it, If there are too many buttons I need it to scroll. However, I don't want to use the standard scrollbar instead I just want to have two buttons at either end of the control one to scroll up and the other down. What is the best way to achieve this?

Is it possible to change the style of the scrollbar so that they are just buttons and not the full horizontal GUI?

3 Answers 3

22

You would normally use a ScrollViewer to achieve this. For example:

<UserControl x:Class="WpfApplication2.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="200" d:DesignWidth="300">
    <ScrollViewer>
        <StackPanel>
            <Button Content="Test"/>
            <Button Content="Test"/>
            <Button Content="Test"/>
            <Button Content="Test"/>
            <Button Content="Test"/>
            <Button Content="Test"/>
            <Button Content="Test"/>
            <Button Content="Test"/>
            <Button Content="Test"/>
            <Button Content="Test"/>
            <Button Content="Test"/>
            <Button Content="Test"/>
        </StackPanel>
    </ScrollViewer>
</UserControl>

Will display a vertical scrollbar automatically when required.

You can change the behaviour by specifying VerticalScrollbarVisibility like this

<ScrollViewer VerticalScrollBarVisibility="Auto">
<ScrollViewer VerticalScrollBarVisibility="Visible">
<ScrollViewer VerticalScrollBarVisibility="Hidden">
<ScrollViewer VerticalScrollBarVisibility="Disabled">

There is of course a HorizontalScrollBarVisibility property as well.

Sign up to request clarification or add additional context in comments.

Comments

1

The way to solve a problem like this is to examine the properties and methods of the control.

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="auto" />
        <RowDefinition Height="*" />
        <RowDefinition Height="auto" />
    </Grid.RowDefinitions>
    <Button Grid.Row="0" Width="40" HorizontalAlignment="Left" Content="Up" Click="clickSVup"/>
    <ScrollViewer x:Name="svBtn" Grid.Row="1" Width="100" HorizontalAlignment="Left" 
                  VerticalScrollBarVisibility="Hidden" HorizontalScrollBarVisibility="Hidden">
        <StackPanel>
            <TextBlock Text="Text 01"/>
            <TextBlock Text="Text 02"/>
            <TextBlock Text="Text 03"/>
            <TextBlock Text="Text 04"/>
            <TextBlock Text="Text 05"/>
            <TextBlock Text="Text 06"/>
            <TextBlock Text="Text 07"/>
            <TextBlock Text="Text 08"/>
            <TextBlock Text="Text 09"/>
            <TextBlock Text="Text 10"/>
            <TextBlock Text="Text 11"/>
            <TextBlock Text="Text 12"/>
            <TextBlock Text="Text 13"/>
            <TextBlock Text="Text 14"/>
            <TextBlock Text="Text 15"/>
            <TextBlock Text="Text 16"/>
            <TextBlock Text="Text 17"/>
            <TextBlock Text="Text 18"/>
            <TextBlock Text="Text 19"/>
        </StackPanel>
    </ScrollViewer>
    <Button Grid.Row="2" Width="40" HorizontalAlignment="Left" Content="Down" Click="clickSVdn"/>
</Grid>



    private void clickSVdn(object sender, RoutedEventArgs e)
    {
        svBtn.PageDown();
    }

    private void clickSVup(object sender, RoutedEventArgs e)
    {
        svBtn.PageUp();
    }

Comments

0

I would advise you to use the standard windows scrollbar instead using some custom buttons for scroll up and scroll down. The users are very used to it for scrolling purposes. If you use two buttons you have to further display something else like scrolling progress. Don't reinvent the wheel :)

1 Comment

the only reason I was thinking about using two buttons is because it is to run on a touchscreen m but I guess I can always just make scroll bars touch friendly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.