Default Thumb Style


   
      
<Window x:Class="ScrollBarCustomThumbSize.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sys="clr-namespace:System;assembly=mscorlib" 
    Title="ScrollBarCustomThumbSize" 
    >
    <Grid>
        <TextBlock Text="Default Thumb size"/>
        <ScrollViewer Height="200" Width="100"
                      HorizontalScrollBarVisibility="Disabled" >
            <StackPanel Background="LightBlue" Width="100">
                <TextBlock FontSize="14" TextWrapping="Wrap">
              Epsum factorial non deposit quid pro quo hic escorol.  Olypian quarrels et gorilla
              congolium sic ad nauseum. Souvlaki ignitus carborundum e pluribus unum. Defacto
              lingo est igpay atinlay.

                </TextBlock>

            </StackPanel>
        </ScrollViewer>

    </Grid>
</Window>

   
    
    
    
    
    
     


Put Canvas into ScrollViewer

   
    

<Window x:Class="SimpleStyles.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="SimpleStyles"
  Background="#F8F8F8">
  <ScrollViewer>
    <WrapPanel>
      <HeaderedItemsControl Header="ScrollViewer">
        <StackPanel>
          <ScrollViewer Width="200" Height="200" Style="{StaticResource LeftScrollViewer}">
            <Canvas Width="400" Height="400">
              <Canvas.Background>
                <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
                  <GradientStop Color="#FFF" Offset="0"/>
                  <GradientStop Color="#AAA" Offset="1"/>
                </LinearGradientBrush>
              </Canvas.Background>
            </Canvas>
          </ScrollViewer>
        </StackPanel>
      </HeaderedItemsControl>
   
    </WrapPanel>
  </ScrollViewer>
</Window>

   
    
    
    
     


Create RoutedCommand from InputGestureCollection

   
  

<Window x:Class="Commands.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Commands">
<Grid>
    <Button VerticalAlignment="Top" 
      HorizontalAlignment="Stretch" 
      Height="27" 
      Click="ExecuteCommandClickEvent" 
      Name="BtnExecuteCommand">Execute Command
  </Button>
  
  </Grid>
</Window>

//File:Window.xaml.cs

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

namespace Commands
{
    public partial class Window1 : Window
    {
       
        RoutedCommand myCmd;

        Window1()
        {
            InputGestureCollection myInputs = new InputGestureCollection();
            myInputs.Add(new KeyGesture(Key.G,ModifierKeys.Control | ModifierKeys.Shift));
            myCmd = new RoutedCommand("Go", typeof(Window1), myInputs);
        }

        private void ExecuteCommandClickEvent(object sender, RoutedEventArgs e)
        {
            myCmd.Execute(sender,null);
        }
    }
}

   
    
     


Routed Event Demo


   
  
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Class="MyNameSpace.RoutedEventDemo.RoutedEventDemo"
        Title="Routed Event Demo">
    <TextBlock Name="txtblk">TextBlock with Context Menu
        <TextBlock.ContextMenu>
            <ContextMenu MenuItem.Click="MenuItemOnClick">
                <MenuItem Header="Red" />
                <MenuItem Header="Orange" />
                <MenuItem Header="Yellow" />
                <MenuItem Header="Green" />
                <MenuItem Header="Blue" />
                <MenuItem Header="Indigo" />
                <MenuItem Header="Violet" />
            </ContextMenu>
        </TextBlock.ContextMenu>
    </TextBlock>
</Window>
//File:Window.xaml.cs

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

namespace MyNameSpace.RoutedEventDemo
{
    public partial class RoutedEventDemo : Window
    {

        public RoutedEventDemo()
        {
            InitializeComponent();
        }
        void MenuItemOnClick(object sender, RoutedEventArgs args)
        {
            string str = (args.Source as MenuItem).Header as string;
            Color clr = (Color)ColorConverter.ConvertFromString(str);
            txtblk.Foreground = new SolidColorBrush(clr);
        }
    }
}

   
    
     


RoutedEvents: Tunneled Key Press


   
  
<Window x:Class="RoutedEvents.TunneledKeyPress"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="TunneledKeyPress" Height="400" Width="400" PreviewKeyDown="SomeKeyPressed" >
  <StackPanel>
    <Label Margin="5" BorderBrush="Black" BorderThickness="1" HorizontalContentAlignment="Stretch"
           PreviewKeyDown="SomeKeyPressed">
      <StackPanel PreviewKeyDown="SomeKeyPressed">
        <TextBlock Margin="3" HorizontalAlignment="Center" PreviewKeyDown="SomeKeyPressed">
          Image and text label
        </TextBlock>
        <Image Source="c:image.jpg" Stretch="None" PreviewKeyDown="SomeKeyPressed"/>
        <DockPanel Margin="10" PreviewKeyDown="SomeKeyPressed"> 
          <TextBlock Margin="3" PreviewKeyDown="SomeKeyPressed">Type here:</TextBlock>
          <TextBox PreviewKeyDown="SomeKeyPressed" KeyDown="SomeKeyPressed"></TextBox>
        </DockPanel>
      </StackPanel>
    </Label>
    <ListBox Margin="5" Name="lstMessages"></ListBox>
    <CheckBox Margin="5" Name="chkHandle">Handle first event</CheckBox>
    <Button Click="cmdClear_Click" HorizontalAlignment="Right" Margin="5" Padding="3">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 TunneledKeyPress : System.Windows.Window
    {
        public TunneledKeyPress()
        {
            InitializeComponent();
        }

        protected int eventCounter = 0;

        private void SomeKeyPressed(object sender, RoutedEventArgs e)
        {
            eventCounter++;
            string message = "#" + eventCounter.ToString() + ":
" +
                " Sender: " + sender.ToString() + "
" +
                " Source: " + e.Source + "
" +
                " Original Source: " + e.OriginalSource + "
" +
                " Event: " + e.RoutedEvent;
            lstMessages.Items.Add(message);
            e.Handled = (bool)chkHandle.IsChecked;
        }

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

   
    
     


RoutedEvents Only Numbers


   
  


<Window x:Class="RoutedEvents.OnlyNumbers"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Only Numbers" Height="300" Width="300">
    <StackPanel Margin="5" PreviewTextInput="pnl_PreviewTextInput" PreviewKeyDown="pnl_PreviewKeyDown">
        <TextBox Margin="3" AcceptsTab="False"></TextBox>
        <TextBox Margin="3"></TextBox>
        <TextBox Margin="3"></TextBox>
    </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 OnlyNumbers : System.Windows.Window
    {
        public OnlyNumbers()
        {
            InitializeComponent();
        }
        private void pnl_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            short val;
            if (!Int16.TryParse(e.Text, out val))
            {
                e.Handled = true;
            }            
        }

        private void pnl_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Space)
            {
                e.Handled = true;
            }
        }
    }
}

   
    
     


RoutedEvents: Mouse Position


   
  



<Window x:Class="RoutedEvents.MousePosition"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MousePosition" Height="300" Width="300">
  <StackPanel>
    <Rectangle Name="rect" MouseMove="MouseMoved" Fill="LightBlue" ></Rectangle>
    <Button Name="cmdCapture" Click="cmdCapture_Click">Capture the Mouse</Button>
    <TextBlock Name="lblInfo"></TextBlock>
  </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 MousePosition : System.Windows.Window
    {
        public MousePosition()
        {
            InitializeComponent();
        }
        private void cmdCapture_Click(object sender, RoutedEventArgs e)
        {
            this.AddHandler(Mouse.LostMouseCaptureEvent,new RoutedEventHandler(this.LostCapture));
            Mouse.Capture(rect);

            cmdCapture.Content = "Mouse captured";
        }

        private void MouseMoved(object sender, MouseEventArgs e)
        {
            Point pt = e.GetPosition(this);
            lblInfo.Text = String.Format("({0},{1}) in window coordinates",pt.X, pt.Y);            
        }

        private void LostCapture(object sender, RoutedEventArgs e)
        {
            lblInfo.Text = "Lost capture";
            cmdCapture.Content = "Capture the Mouse";
        }
    }
}