Scroll Buttons with ScrollViewer : ScrollViewer « Windows Presentation Foundation « C# / CSharp Tutorial






using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

    class MainClass : Window
    {
        [STAThread]
        public static void Main()
        {
            Application app = new Application();
            app.Run(new MainClass());
        }
        public MainClass()
        {
            SizeToContent = SizeToContent.Width;
            AddHandler(Button.ClickEvent, new RoutedEventHandler(ButtonOnClick));

            ScrollViewer scroll = new ScrollViewer();
            Content = scroll;

            StackPanel stack = new StackPanel();
            stack.Margin = new Thickness(5);
            scroll.Content = stack; 

            for (int i = 0; i < 5; i++)
            {
                Button btn = new Button();
                btn.Name = "Button" + (i + 1);
                btn.Content = btn.Name + " says 'Click me'";
                btn.Margin = new Thickness(5);

                stack.Children.Add(btn);
            }
        }
        void ButtonOnClick(object sender, RoutedEventArgs args)
        {
            Button btn = args.Source as Button;
            if (btn != null)
                MessageBox.Show(btn.Name + " has been clicked","Button Click");
        }
    }








24.53.ScrollViewer
24.53.1.Put Canvas into ScrollViewerPut Canvas into ScrollViewer
24.53.2.Default Thumb StyleDefault Thumb Style
24.53.3.StackPanel in a ScrollViewerStackPanel in a ScrollViewer
24.53.4.ScrollViewer and Big EllipseScrollViewer and Big Ellipse
24.53.5.Use the content-scrolling methods of the ScrollViewer classUse the content-scrolling methods of the ScrollViewer class
24.53.6.Scroll Buttons with ScrollViewer