Path Animation with DoubleAnimationUsingPath, AutoReverse

image_pdfimage_print


   
  

<Window x:Class="WpfApplication1.PathAnimationExample"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Path Animation" Height="500" Width="518">
    <Canvas Margin="5">

        <Polyline Points="0,345,96,345,320,432,500,432" Stroke="Gray"
      StrokeThickness="5" />
        <Path>
            <Path.Data>
                <PathGeometry x:Name="path2" Figures="M0,292 L75,292 300,380,449,380" />
            </Path.Data>
        </Path>
        <Ellipse Name="circle2" Stroke="DarkGoldenrod" Canvas.Left="0"
      Canvas.Top="293" Width="50" Height="50">
            <Ellipse.Fill>
                <LinearGradientBrush>
                    <GradientStop Color="DarkGoldenrod" Offset="0.5" />
                    <GradientStop Color="Gold" Offset="0.5" />
                </LinearGradientBrush>
            </Ellipse.Fill>
            <Ellipse.RenderTransform>
                <RotateTransform x:Name="circle2Rotate" CenterX="25" CenterY="25" />
            </Ellipse.RenderTransform>
        </Ellipse>
    </Canvas>
</Window>

//File:Window.xaml.cs

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

namespace WpfApplication1
{
    public partial class PathAnimationExample : Window
    {
        public PathAnimationExample()
        {
            InitializeComponent();

            path2.Freeze(); // For performance benefits. 
            DoubleAnimationUsingPath daPath = new DoubleAnimationUsingPath();
            daPath.Duration = TimeSpan.FromSeconds(5);
            daPath.RepeatBehavior = RepeatBehavior.Forever;
            daPath.AccelerationRatio = 0.6;
            daPath.DecelerationRatio = 0.4;
            daPath.AutoReverse = true;
            daPath.PathGeometry = path2;
            daPath.Source = PathAnimationSource.X;
            circle2.BeginAnimation(Canvas.LeftProperty, daPath);

            daPath = new DoubleAnimationUsingPath();
            daPath.Duration = TimeSpan.FromSeconds(5);
            daPath.RepeatBehavior = RepeatBehavior.Forever;
            daPath.AccelerationRatio = 0.6;
            daPath.DecelerationRatio = 0.4;
            daPath.AutoReverse = true;
            daPath.PathGeometry = path2;
            daPath.Source = PathAnimationSource.Y;
            circle2.BeginAnimation(Canvas.TopProperty, daPath);

        }
    }
}

   
    
     


Drag Items from a List and Drop Them on a Canvas

image_pdfimage_print


   
  

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF " Height="300" Width="300">
    <DockPanel LastChildFill="True" >
        <ListBox DockPanel.Dock="Left" Name="lstLabels">
            <ListBox.Resources>
                <Style TargetType="{x:Type ListBoxItem}">
                    <EventSetter Event="PreviewMouseLeftButtonDown" Handler="ListBoxItem_PreviewMouseLeftButtonDown"/>    
                    <EventSetter Event="PreviewMouseMove" Handler="ListBoxItem_PreviewMouseMove"/>
                </Style>
            </ListBox.Resources>
            <ListBoxItem>A</ListBoxItem>
            <ListBoxItem>B</ListBoxItem>
            <ListBoxItem>C</ListBoxItem>
            <ListBoxItem>D</ListBoxItem>
            <ListBoxItem>E</ListBoxItem>
            <ListBoxItem>F</ListBoxItem>
            <ListBoxItem>G</ListBoxItem>
            <ListBoxItem>H</ListBoxItem>
        </ListBox>
        <Canvas AllowDrop="True" Background="Red"
                DragEnter="cvsSurface_DragEnter" Drop="cvsSurface_Drop" 
                Name="cvsSurface" >
        </Canvas>
    </DockPanel>
</Window>
//File:Window.xaml.cs

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

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        private ListBoxItem draggedItem;
        private Point startDragPoint;

        public Window1()
        {
            InitializeComponent();
        }
        private void cvsSurface_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Text))
            {
                e.Effects = DragDropEffects.Copy;
            }
            else
            {
                e.Effects = DragDropEffects.None;
            }
        }
        private void cvsSurface_Drop(object sender, DragEventArgs e)
        {
            Label newLabel = new Label();
            newLabel.Content = e.Data.GetData(DataFormats.Text);

            cvsSurface.Children.Add(newLabel);
            Canvas.SetLeft(newLabel,100);
            Canvas.SetTop(newLabel, 200);
        }
        private void ListBoxItem_PreviewMouseLeftButtonDown(object sender,MouseButtonEventArgs e)
        {
            draggedItem = sender as ListBoxItem;
            startDragPoint = e.GetPosition(null);
        }
        private void ListBoxItem_PreviewMouseMove(object sender,MouseEventArgs e)
        {
            Point position = e.GetPosition(null);
            DragDrop.DoDragDrop(draggedItem, draggedItem.Content,DragDropEffects.Copy);
        }
    }
}

   
    
     


Using a DrawingBrush as an Opacity Mask

image_pdfimage_print


   
 

<Window x:Class="Workspace.DockExample"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Workspace" Width="640" Height="480">
    <Rectangle Height="150" Width="200" Stroke="Black" StrokeThickness="1">
      <Rectangle.Fill>
        <DrawingBrush>
          <DrawingBrush.Drawing>
            <GeometryDrawing>
              <GeometryDrawing.Brush>
                <RadialGradientBrush>
                  <RadialGradientBrush.GradientStops>
                    <GradientStop Offset="0" Color="Black"/>
                    <GradientStop Offset="1" Color="Transparent"/>
                  </RadialGradientBrush.GradientStops>
                </RadialGradientBrush>
              </GeometryDrawing.Brush>
              <GeometryDrawing.Geometry>
                <RectangleGeometry Rect="0.05,0.05 0.9,0.9" />
              </GeometryDrawing.Geometry>
              <GeometryDrawing.Pen>
                <Pen Thickness="0.1" Brush="Black" />
              </GeometryDrawing.Pen>
            </GeometryDrawing>
          </DrawingBrush.Drawing>
        </DrawingBrush>
      </Rectangle.Fill>
    </Rectangle>

</Window>

   
     


DrawingBrush and DrawingGroup

image_pdfimage_print


   
 
<Window x:Class="Workspace.DockExample"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Workspace" Width="640" Height="480">
        <Rectangle
          Width="100" Height="100"
          Stroke="Black" StrokeThickness="1">
          <Rectangle.Fill>
            <DrawingBrush Viewport="0,0,0.5,0.5" TileMode="Tile">
              <DrawingBrush.Drawing>
                <DrawingGroup>
                  <DrawingGroup.Children>
                    <GeometryDrawing Geometry="M0,0.1 L0.1,0 1,0.9, 0.9,1z"
                      Brush="Red" />
                    <GeometryDrawing Geometry="M0.9,0 L1,0.1 0.1,1 0,0.9z"
                      Brush="Gray" />

                  </DrawingGroup.Children>
                </DrawingGroup>
              </DrawingBrush.Drawing>
            </DrawingBrush>
          </Rectangle.Fill>
        </Rectangle>
</Window>

   
     


Grid with ContextMenu

image_pdfimage_print


   
    
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid Background="Transparent">
  <Grid.ContextMenu>
    <ContextMenu>
      <MenuItem Header="Foo" />
      <MenuItem Header="Bar" />
    </ContextMenu>
  </Grid.ContextMenu>


</Grid>
</Page>

   
    
    
    
     


Display a Context Menu with Opacity

image_pdfimage_print


   
  
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF" Height="100" Width="300">
    <Grid>
        <TextBox FontSize="16"  Height="23" Name="txtTextBox" >
            <TextBox.ContextMenu>
                <ContextMenu HasDropShadow="True" Opacity=".8">
                    <MenuItem Command="Cut" Header="Cu_t" />
                    <MenuItem Command="Copy" Header="_Copy" />
                    <MenuItem Command="Paste" Header="_Paste" />
                    <Separator/>
                    <MenuItem Click="SelectAll_Click" Header="_Select All" />
                    <MenuItem Click="Clear_Click" Header="_Clear" />
                    <Separator/>
                    <MenuItem Header="Format">
                        <MenuItem Click="TextStyle_Click" Header="_Normal" Name="miNormal"></MenuItem>
                        <MenuItem Click="TextStyle_Click" FontWeight="Bold" Header="_Bold" Name="miBold"></MenuItem>
                        <MenuItem Click="TextStyle_Click" FontStyle="Italic" Header="_Italic" Name="miItalic"></MenuItem>
                    </MenuItem>
                </ContextMenu>
            </TextBox.ContextMenu>
            A TextBox control with ContextMenu.
        </TextBox>
    </Grid>
</Window>
//File:Window.xaml.cs
using System.Windows;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        private void Clear_Click(object sender, RoutedEventArgs e)
        {
            txtTextBox.Clear();
        }
        private void SelectAll_Click(object sender, RoutedEventArgs e)
        {
            txtTextBox.SelectAll();
        }
        private void TextStyle_Click(object sender, RoutedEventArgs e)
        {
            if (sender == miNormal)
            {
                txtTextBox.FontWeight = FontWeights.Normal;
                txtTextBox.FontStyle = FontStyles.Normal;
            }
            else if (sender == miBold)
            {
                txtTextBox.FontWeight = FontWeights.Bold;
            }
            else if (sender == miItalic)
            {
                txtTextBox.FontStyle = FontStyles.Italic;
            }
        }
    }
}

   
    
     


Control template: event trigger, border and text

image_pdfimage_print


   
      
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Page.Resources>
        <ControlTemplate x:Key="btnCustom" TargetType="{x:Type Button}">
            <Border Name="border" BorderThickness="3" BorderBrush="Blue"
                    Background="{TemplateBinding Foreground}">

                <TextBlock Name="txtblk" 
                           FontStyle="Italic" 
                           Text="{TemplateBinding Content}"
                           Margin="{TemplateBinding Padding}"
                           Foreground="{TemplateBinding Background}" />
            </Border>

            <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter TargetName="border"
                            Property="CornerRadius" Value="12" />
                    <Setter TargetName="txtblk"
                            Property="FontWeight" Value="Bold" />
                </Trigger>

                <Trigger Property="IsPressed" Value="True">
                    <Setter TargetName="border"
                            Property="Background" 
                            Value="{Binding Path=Background}" />
                    <Setter TargetName="txtblk"
                            Property="Foreground"
                            Value="{Binding Path=Foreground}" />
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Page.Resources>

    <StackPanel>
        <Button Template="{StaticResource btnCustom}"
                HorizontalAlignment="Center" Margin="24"
                FontSize="24"   >
            Button
        </Button>
    </StackPanel>
</Page>