Use the content-scrolling methods of the ScrollViewer class

image_pdfimage_print



ScrollViewer Content Scrolling Methods

















//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Automation.Provider;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Navigation;
using System.Text;

namespace ScrollViewer_Methods
{
public partial class Window1 : Window
{
private void onLoad(object sender, System.EventArgs e)
{
StringBuilder myStringBuilder = new StringBuilder(400);
for (int i = 0; i < 100; i++) { myStringBuilder.Append("this is a test "); } txt1.Text = myStringBuilder.ToString(); } private void svLineUp(object sender, RoutedEventArgs e) { sv1.LineUp(); } private void svLineDown(object sender, RoutedEventArgs e) { sv1.LineDown(); } private void svLineRight(object sender, RoutedEventArgs e) { sv1.LineRight(); } private void svLineLeft(object sender, RoutedEventArgs e) { sv1.LineLeft(); } private void svPageUp(object sender, RoutedEventArgs e) { sv1.PageUp(); } private void svPageDown(object sender, RoutedEventArgs e) { sv1.PageDown(); } private void svPageRight(object sender, RoutedEventArgs e) { sv1.PageRight(); } private void svPageLeft(object sender, RoutedEventArgs e) { sv1.PageLeft(); } } } [/csharp]

Add Separator to StackPanel

image_pdfimage_print


   
    

<Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="193" Width="197">
    <Grid>
        <StackPanel Name="StackPanel1">
            <Button Height="23" Name="Button1">Button</Button>
            <Button Height="23" Name="Button2" Width="Auto">Button</Button>
            <Separator Height="5" Name="Separator1" Width="Auto" />
            <Button Height="23" Name="Button3" >Button</Button>
        </StackPanel>
    </Grid>
</Window>

   
    
    
    
     


Use Slider to control the ScaleTransform

image_pdfimage_print


   
    

<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Label Content="ScaleX" />
    <ScrollBar Name="xscroll" Orientation="Horizontal" Value="1" Minimum="-20" Maximum="20" />
    <TextBlock HorizontalAlignment="Center" Text="{Binding ElementName=xscroll, Path=Value}" />

    <Label Content="ScaleY" />
    <ScrollBar Name="yscroll" Orientation="Horizontal" Value="1" Minimum="-20" Maximum="20" />
    <TextBlock HorizontalAlignment="Center" Text="{Binding ElementName=yscroll, Path=Value}" />

    <Label Content="CenterX" />
    <ScrollBar Name="xcenter" Orientation="Horizontal" Value="0" Minimum="-100" Maximum="100" /> 
    <TextBlock HorizontalAlignment="Center" Margin="12" Text="{Binding ElementName=xcenter, Path=Value}" />

    <Label Content="CenterY" />
    <ScrollBar Name="ycenter" Orientation="Horizontal" Value="0" Minimum="-100" Maximum="100" />
    <TextBlock HorizontalAlignment="Center" Margin="12" Text="{Binding ElementName=ycenter, Path=Value}" />

    <Canvas>
        <Button Name="btn" Content="Button" Canvas.Left="100" Canvas.Top="100">
            <Button.RenderTransform>
                <ScaleTransform
                    ScaleX="{Binding ElementName=xscroll, Path=Value}"
                    ScaleY="{Binding ElementName=yscroll, Path=Value}" 
                    CenterX="{Binding ElementName=xcenter, Path=Value}"
                    CenterY="{Binding ElementName=ycenter, Path=Value}" />
            </Button.RenderTransform>
        </Button>

        <StackPanel>
            <TextBlock Text="{Binding ElementName=btn, Path=ActualWidth}" />
            <TextBlock Text="::" />
            <TextBlock Text="{Binding ElementName=btn, Path=ActualHeight}" />
        </StackPanel>
    </Canvas>
</StackPanel>

   
    
    
    
     


Use Slider to control SkewTransform

image_pdfimage_print


   
    
<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Label Content="AngleX" />
    <ScrollBar Name="xscroll" Orientation="Horizontal" Value="0" Minimum="-90" Maximum="90" />
    <TextBlock HorizontalAlignment="Center" Text="{Binding ElementName=xscroll, Path=Value}" />

    <Label Content="AngleY" />
    <ScrollBar Name="yscroll" Orientation="Horizontal" Value="0" Minimum="-90" Maximum="90" />
    <TextBlock HorizontalAlignment="Center" Text="{Binding ElementName=yscroll, Path=Value}" />

    <Label Content="CenterX" />
    <ScrollBar Name="xcenter" Orientation="Horizontal" Value="0" Minimum="-100" Maximum="100" /> 
    <TextBlock HorizontalAlignment="Center" Text="{Binding ElementName=xcenter, Path=Value}" />

    <Label Content="CenterY" />
    <ScrollBar Name="ycenter" Orientation="Horizontal" Value="0" Minimum="-100" Maximum="100" />
    <TextBlock HorizontalAlignment="Center" Margin="12" Text="{Binding ElementName=ycenter, Path=Value}" />

    <Canvas>
        <Button Name="btn" Content="Button" Canvas.Left="100" Canvas.Top="100">
            <Button.RenderTransform>
                <SkewTransform
                    AngleX="{Binding ElementName=xscroll, Path=Value}"
                    AngleY="{Binding ElementName=yscroll, Path=Value}" 
                    CenterX="{Binding ElementName=xcenter, Path=Value}"
                    CenterY="{Binding ElementName=ycenter, Path=Value}" />
            </Button.RenderTransform>
        </Button>

        <StackPanel>
            <TextBlock Text="{Binding ElementName=btn, Path=ActualWidth}" />
            <TextBlock Text="::" />
            <TextBlock Text="{Binding ElementName=btn, Path=ActualHeight}" />
        </StackPanel>
    </Canvas>
</StackPanel>

   
    
    
    
     


RoutedEvents: Focus event

image_pdfimage_print


   
  

<Window x:Class="RoutedEvents.Focus"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Focus" Height="250" Width="300">
  <StackPanel Margin="5">
    <Button Margin="5" Padding="30" Name="cmdFocus">Focused</Button>
    <Button Margin="5" Padding="30">Not Focused</Button>
    </StackPanel>
</Window>


//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace RoutedEvents
{
    public partial class Focus : System.Windows.Window
    {
        public Focus()
        {
            InitializeComponent();
        }
        protected override void OnActivated(EventArgs e)
        {
            base.OnActivated(e);
            cmdFocus.Focus();
        }
    }
}

   
    
     


RoutedEvents: Key Modifiers

image_pdfimage_print


   
  
<Window x:Class="RoutedEvents.KeyModifiers"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="KeyModifiers" Height="300" Width="300">
  <StackPanel Margin="5">
    <TextBox KeyDown="KeyEvent"></TextBox>
    <TextBlock Name="lblInfo"></TextBlock>
    <Button Click="CheckShift">Check Current Shift State</Button>
  </StackPanel>
</Window>

//File:Window.xaml.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace RoutedEvents
{
    public partial class KeyModifiers : System.Windows.Window
    {
        public KeyModifiers()
        {
            InitializeComponent();
        }

        private void KeyEvent(object sender, KeyEventArgs e)
        {
            
            lblInfo.Text = "Modifiers: " + e.KeyboardDevice.Modifiers.ToString();

            if ((e.KeyboardDevice.Modifiers &amp; ModifierKeys.Control) == ModifierKeys.Control)
            {
                lblInfo.Text += "
You held the Control key.";                   
            }
        }

        private void CheckShift(object sender, RoutedEventArgs e)
        {
            if (Keyboard.IsKeyDown(Key.LeftShift))
            {
                lblInfo.Text = "The left Shift is held down.";                   
            }
            else
            {
                lblInfo.Text = "The left Shift is not held down.";                   
            }
            
        }
    }
}

   
    
     


RoutedEvents: Key Press Events

image_pdfimage_print


   
  
<Window x:Class="RoutedEvents.KeyPressEvents"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="KeyPressEvents" Height="400" Width="400" >
    <StackPanel>
          <DockPanel Margin="5">
            <TextBlock Margin="3" >Type here:</TextBlock>
            <TextBox PreviewKeyDown="KeyEvent" KeyDown="KeyEvent" 
                     PreviewKeyUp="KeyEvent" KeyUp="KeyEvent"
                     PreviewTextInput="TextInput"
                     TextChanged="TextChanged"></TextBox>
          </DockPanel>
      <ListBox Margin="5" Name="lstMessages"></ListBox>
      <CheckBox Margin="5" Name="chkIgnoreRepeat">Ignore Repeated Keys</CheckBox>
      <Button Click="cmdClear_Click" HorizontalAlignment="Right">Clear List</Button>
    </StackPanel>
</Window>


//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace RoutedEvents
{
    public partial class KeyPressEvents : System.Windows.Window
    {
        public KeyPressEvents()
        {
            InitializeComponent();
        }
                
        private void KeyEvent(object sender, KeyEventArgs e)
        {
            if ((bool)chkIgnoreRepeat.IsChecked &amp;&amp; e.IsRepeat) return;
            
            string message = "Event: " + e.RoutedEvent + " " + " Key: " + e.Key;
            lstMessages.Items.Add(message);            
        }

        private void TextInput(object sender, TextCompositionEventArgs e)
        {
            string message = "Event: " + e.RoutedEvent + " " + " Text: " + e.Text;
            lstMessages.Items.Add(message);
        }

        private void TextChanged(object sender, TextChangedEventArgs e)
        {
            string message = "Event: " + e.RoutedEvent;
            lstMessages.Items.Add(message);
        }

        private void cmdClear_Click(object sender, RoutedEventArgs e)
        {            
            lstMessages.Items.Clear();
        }
    }
}