locked
How to adjust C# scrollbar RRS feed

  • Question

  • Hi Guys,

    How to adjust C# scrollbar to make something like this (please see image below). Thanks a lot.


    • Edited by Richard0012 Thursday, June 18, 2015 8:27 PM
    Thursday, June 18, 2015 8:24 PM

Answers

  • Hi Richard0012,

    In my experience, custom ListBox with Scrollbar would achieve your target. You could refer to this blog about “WPF Custom ListBox with Scrollbar on the Background”, I have tested this sample, it work well, it would meet your requirements, here is the link: http://www.codeproject.com/Articles/64129/WPF-Custom-ListBox-with-Scrollbar-on-the-Backgroun

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Best Regards,

    Xavier Eoro


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Richard0012 Saturday, June 20, 2015 7:36 PM
    Friday, June 19, 2015 3:32 AM

All replies

  • I really think that trying to retemplate the scrollbar to what you want is a path you do not want to follow.  The functionality of the scrollbar is internal so that what you want is difficult.

    The way to go is a UserControl.  The control will consist of a grid with three columns , one for the left arrow, one for the right arrow and a ListBox to display the "news stories".

    You will need to have some method of obtaining the "stories"and the graphics for the stories.  You can then using the buttons of either end "scroll" the stories by paging the data from the above method.


    Lloyd Sheen

    Thursday, June 18, 2015 9:18 PM
  • I have gen'ed up a sample (in VB) that shows how (with lots of implementation to be done).

    UserControl XAML

    <UserControl x:Class="StoryBarUC"
                 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="50" d:DesignWidth="400">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="50"></ColumnDefinition>
                <ColumnDefinition></ColumnDefinition>
                <ColumnDefinition Width="50"></ColumnDefinition>
            </Grid.ColumnDefinitions>  
            
            <Button x:Name="LeftButton" Click="LeftButton_Click">Left</Button>
            <Button x:Name="RightButton" Grid.Column="2" Click="RightButton_Click">Right</Button>
            
            <ListBox x:Name="theItems" ItemsSource="{Binding theItemsList}" Grid.Column="1">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Button Content="{Binding Title}" Click="Button_Click" Width="50"></Button>
                    </DataTemplate>
                </ListBox.ItemTemplate>
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal" ></StackPanel>
                    </ItemsPanelTemplate> 
                </ListBox.ItemsPanel>
            </ListBox>
        </Grid>
    </UserControl>
    

    UserControl Code Behind:

    Imports System.ComponentModel
    
    Public Class StoryBarUC
        Implements INotifyPropertyChanged
    
        Private pageSize As Integer = 4
        Private currentPage As Integer
        Private maxPages As Integer
    
        Private _tempItemsList As List(Of NewsStory)
        Public Property tempItemsList() As List(Of NewsStory)
            Get
                Return _tempItemsList
            End Get
            Set(ByVal value As List(Of NewsStory))
                DataContext = Me
                _tempItemsList = value
                currentPage = 0
                maxPages = tempItemsList.Count / pageSize
                moveToDisplay()
                RaiseChanged("theItemsList")
            End Set
        End Property
    
        Private _theItemsList As List(Of NewsStory)
        Public Property theItemsList() As List(Of NewsStory)
            Get
                Return _theItemsList
            End Get
            Set(ByVal value As List(Of NewsStory))
                _theItemsList = value
                RaiseChanged("theItemsList")
            End Set
        End Property
    
        Private Sub moveToDisplay()
            Dim skipcount As Integer = pageSize * currentPage
    
            theItemsList = (From story In tempItemsList Select story Skip skipcount Take pageSize).ToList
        End Sub
    
        Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
            Dim newstor As NewsStory = sender.datacontext
            MsgBox(newstor.Link)
        End Sub
    
        Private Sub LeftButton_Click(sender As Object, e As RoutedEventArgs)
            currentPage -= 1
            If currentPage < 0 Then
                currentPage = 0
            End If
            moveToDisplay()
        End Sub
    
        Private Sub RightButton_Click(sender As Object, e As RoutedEventArgs)
            currentPage += 1
            If currentPage > maxPages Then
                currentPage = maxPages
            End If
            moveToDisplay()
        End Sub
    
        Private Sub RaiseChanged(p1 As String)
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(p1))
        End Sub
    
        Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged
    
    
    End Class
    

    MainWindow(test window) XAML:

    <Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:StoryBar"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
        <Grid>
            <local:StoryBarUC x:Name="theStoryBar" HorizontalAlignment="Left" Margin="40,98,0,0" VerticalAlignment="Top" Width="400"/>
        </Grid>
    </Window>
    

    MainWindow Code Behind:

    Imports System.ComponentModel
    
    Class MainWindow
        Implements INotifyPropertyChanged
    
    
        Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
            Dim temp As New List(Of NewsStory)
            For i As Integer = 0 To 25
                Dim t As New NewsStory
                t.Title = i.ToString
                t.Link = "Link to story " + i.ToString
                temp.Add(t)
            Next
            theStoryBar.tempItemsList = temp
            DataContext = Me
        End Sub
    
    
        Private Sub RaiseChanged(p1 As String)
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(p1))
        End Sub
    
        Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged
    End Class
    

    Test Class:

    Public Class NewsStory
        Implements INotifyPropertyChanged
    
        Private _Title As String
        Public Property Title() As String
            Get
                Return _Title
            End Get
            Set(ByVal value As String)
                _Title = value
                RaiseChanged("Title")
            End Set
        End Property
    
        Private _Link As String
        Public Property Link() As String
            Get
                Return _Link
            End Get
            Set(ByVal value As String)
                _Link = value
                RaiseChanged("Link")
            End Set
        End Property
    
    
        Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged
    
        Private Sub RaiseChanged(p1 As String)
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(p1))
        End Sub
    
    End Class


    Lloyd Sheen

    Thursday, June 18, 2015 10:19 PM
  • Hi Richard0012,

    In my experience, custom ListBox with Scrollbar would achieve your target. You could refer to this blog about “WPF Custom ListBox with Scrollbar on the Background”, I have tested this sample, it work well, it would meet your requirements, here is the link: http://www.codeproject.com/Articles/64129/WPF-Custom-ListBox-with-Scrollbar-on-the-Backgroun

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Best Regards,

    Xavier Eoro


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Richard0012 Saturday, June 20, 2015 7:36 PM
    Friday, June 19, 2015 3:32 AM
  • Thank you very much, Lloyd, for your help.

    But I am new in programming, not familiar with VB.

    So I guess I will mark Xavier's answer as answered.

    Thanks again, and have a nice weekend.

    Sincerely,

    Richard.

    Saturday, June 20, 2015 7:34 PM
  • Thank you very much, Xavier.

    I will study your paper tonight.

    Have a nice weekend.

    Sincerely,

    Richard.

    Saturday, June 20, 2015 7:36 PM