Use Slider to control the Transformation

image_pdfimage_print


   
    


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

    <Label Content="Angle" />
    <ScrollBar Name="angle" Orientation="Horizontal" Value="0" Minimum="0" Maximum="360" />
    <TextBlock HorizontalAlignment="Center" Text="{Binding ElementName=angle, 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>
                <RotateTransform
                    Angle="{Binding ElementName=angle, 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>

   
    
    
    
     


Bind Slider value to TextBlock

image_pdfimage_print


   
    



<Window  
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="SDKSample.Pane1">

    <StackPanel>
      <TextBlock Margin="0,10,0,0"  Foreground="Blue">
      <TextBlock Text="(Height,Width) = "/>
      <TextBlock Text="{Binding ElementName=RectangleHeight,Path=Value}"/>
      ,
      <TextBlock Text="{Binding ElementName=RectangleWidth,Path=Value}"/>
    </TextBlock>

    <TextBlock Margin="10,10,0,0">Height</TextBlock>
    <Slider Name="RectangleHeight"  Margin="10, 0, 0, 0" 
      Width="100" Orientation="Horizontal" HorizontalAlignment="Left" 
      Value="50" Minimum="0" Maximum="200" 
      SmallChange="1" LargeChange="10"
      TickPlacement="BottomRight" TickFrequency="10"/>
    <TextBlock Margin="10,0,0,0">Width</TextBlock>
    <Slider Name="RectangleWidth" Margin="10, 0, 0, 0" 
      Width="100" Orientation="Horizontal" HorizontalAlignment="Left" 
      Value="50" Minimum="0" Maximum="200"
      SmallChange="1" LargeChange="10"
      TickPlacement="BottomRight" TickFrequency="10"/>
        <Rectangle Fill="Blue" HorizontalAlignment="Left" 
               Margin="50,20,0,0" 
               Height="{Binding ElementName=RectangleHeight,Path=Value}" 
               Width="{Binding ElementName=RectangleWidth,Path=Value}"/>
    </StackPanel>

</Window>
  

   
    
    
    
     


Use Slider to control the Blur

image_pdfimage_print


   
    
<Window x:Class="BitmapEffects.Window4"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Bitmap Effects" Height="538" Width="600"
  >
    <StackPanel>
        <Ellipse Height="100" Width="100" Fill="Red" Stroke="Black">
            <Ellipse.BitmapEffect>
                <BlurBitmapEffect 
            Radius="{Binding Path=Value, ElementName=sliderBlur}"
              />
            </Ellipse.BitmapEffect>
        </Ellipse>
        <TextBlock>Blur</TextBlock>
        <Slider Minimum="0" Maximum="10" Name="sliderBlur" Value="14"/>
        <TextBox Text="{Binding ElementName=sliderBlur, Path=Value}"/>

    </StackPanel>
</Window>

   
    
    
    
     


RoutedEvents: Tunneled Key Press

image_pdfimage_print


   
  
<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();
        }
    }
}

   
    
     


Routed Event Demo

image_pdfimage_print


   
  
<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);
        }
    }
}

   
    
     


Create RoutedCommand from InputGestureCollection

image_pdfimage_print
   
  

<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);
        }
    }
}

   
    
     


Put Canvas into ScrollViewer

image_pdfimage_print
   
    

<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>