ScrollViewer Example

scrollViewer.jpg

A ScrollViewer can be added around practically anything in WPF.

Here is a very simple example of taking a StackPanel and wrapping it inside a ScrollViewer. I set the Scrollbars to Auto so that they will automatically display when needed. The ApplyScrollViewerStrategy may be used to set colors on the commonly styled elements of the Scrollbars.

One gotcha to note as that you need to use the ScrollViewer's own Initialize method. Don't get confused and accidentally use ContentControl's Initialize methods. (A ScrollViewer inherits from ContentControl but it has its own specific Initialize extension method.)

ScrollViewer scw = new ScrollViewer();
scw.Initialize(350D, 275D, ContainerType.ContentControlPanel, new Thickness(2), null, null);
scw.ApplyScrollViewerStrategy("Blue", "Red", "Green");

scw.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
scw.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;

scw.BeginComposite<ScrollViewer>()
           .AddExisting<StackPanel, ScrollViewer>(0, 0, myStackPanel)
           .EndComposite<ScrollViewer, ScrollViewerArgs>(null);

If you want to be more fine-grained in how you set the colors on individual elements of the Scrollbars, you may do so in the BeginSettings . . . EndSettings chain of the ScrollViewer.

scvw = new ScrollViewer();

scvw.Initialize(400D, 305.0D, ContainerType.Grid, new Thickness(10, 10, 10, 100), 4, 3);
scvw.SetCompositeGridDimensions<ScrollViewer>(385D, 455.0D, 1, 1, 400D, GridUnitType.Pixel, 430D, GridUnitType.Pixel);

scvw.SetBackgroundColor(Brushes.WhiteSmoke);

scvw.BeginSettings<ScrollViewer>()                ////VERTICAL SCROLLBAR
                .SetVerticalScrollBarColor(Brushes.WhiteSmoke)
                .SetVerticalScrollUpButtonColors("WhiteSmoke", "LawnGreen", "Green", "Green", "Green", "0", "Gray", new Thickness(1), "Black", "LawnGreen", "M 0 4 L 7 4 L 3.5 0 Z")
                .SetVerticalPageUpButtonColors("#DDDDDD", "Gray", "Gray", "#DDDDDD") //, "2", "White", new Thickness(3, 3, 3, 3))
                .SetVerticalPageDownButtonColors("#DDDDDD", "#DDDDDD", "Gray", "Gray") //,"5", "Gray", new Thickness(3, 3, 3, 3))
                .SetVerticalScrollDownButtonColors("WhiteSmoke", "LawnGreen", "Green", "Green", "Green", "0", "Gray", new Thickness(1), "Black", "LawnGreen", "M 0 0 L 3.5 4 L 7 0 Z")
                .SetVerticalScrollThumbColors("ForestGreen", "LawnGreen", "LawnGreen", "#DDDDDD", "1", "1")

                //HORIZONTAL SCROLLBAR
                .SetHorizontalScrollBarColor(Brushes.WhiteSmoke)
                .SetHorizontalScrollRightButtonColors("#BBB", "Blue", "Red", "Red", "Red", "0", "#FFEE", new Thickness(1), "Black", "Orange", "M 0 0 L 4 4 L 0 8 Z")
                .SetHorizontalPageRightButtonColors("#DDDDDD", "#DDDDDD", "Gray", "Gray") //, "#FFEE", "0", new Thickness(0, 0, 0, 0))
                .SetHorizontalPageLeftButtonColors("#DDDDDD", "Gray", "Gray", "#DDDDDD") // , "#FFEE", "0", new Thickness(0, 0, 0, 0))
                .SetHorizontalScrollLeftButtonColors("#BBB", "Blue", "Red", "Red", "Red", "0", "#FFEE", new Thickness(1), "Black", "Orange", "M 4 0 L 4 8 L 0 4 Z")
                .SetHorizontalScrollThumbColors("#0088FF", "#0088FF", "Gray", "#DDDDDD", "1", "1")

                .SetScrollViewerCornerRectangleColor(Brushes.White)

                .EndSettings<ScrollViewer>();

Last edited Apr 5, 2014 at 8:41 PM by stagathome0069, version 9

Comments

No comments yet.