NavigationService.GetNavigationService


   
  

<Page x:Class="NavigationApplication.Page3"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Page3" WindowTitle="Page3" Loaded="Init">
  <StackPanel Margin="3" Name="pnl">
    <TextBlock Margin="3" TextWrapping="Wrap">
      This is a Page3.xaml.
      Click <Hyperlink NavigateUri="Menu.xaml">here</Hyperlink> to go to the Menu.
    </TextBlock>


  </StackPanel>
</Page>
//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.Navigation;
using System.Windows.Shapes;

namespace NavigationApplication
{
    public partial class Page3 : System.Windows.Controls.Page
    {
        public Page3()
        {
            InitializeComponent();
        }

        private void Init(object sender, EventArgs e)
        {
            NavigationService nav = NavigationService.GetNavigationService(this);
            while (nav.CanGoBack)
            {
                ;
            }
        }
    }
}

   
    
     


Hit Result Behavior


   
  
<Window x:Class="WpfApplication1.HitTestExample"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="WpfApplication1" Height="300" Width="300">


  <Canvas x:Name="canvas1"
    MouseLeftButtonDown="OnMouseLeftButtonDown">
    <Rectangle Canvas.Left="20" Canvas.Top="20" Width="100"
      Height="60" Stroke="Black" Fill="LightBlue" Opacity="0.7" />

    <Rectangle Canvas.Left="70" Canvas.Top="50" Width="100"
      Height="60" Stroke="Black" Fill="LightBlue" Opacity="0.7" />


    <Rectangle Canvas.Left="150" Canvas.Top="80" Width="100"
      Height="60" Stroke="Black" Fill="LightBlue" Opacity="0.7" />


    <Rectangle Canvas.Left="20" Canvas.Top="100" Width="50"
      Height="50" Stroke="Black" Fill="LightBlue" Opacity="0.7" />


    <Rectangle Canvas.Left="40" Canvas.Top="60" Width="50"
      Height="50" Stroke="Black" Fill="LightBlue" Opacity="0.7" />


    <Rectangle Canvas.Left="30" Canvas.Top="130" Width="50"
      Height="50" Stroke="Black" Fill="LightBlue" Opacity="0.7" />


  </Canvas>
</Window>

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


namespace WpfApplication1
{
    public partial class HitTestExample : Window
    {
        private List<Rectangle> hitList = new List<Rectangle>();
        private EllipseGeometry hitArea = new EllipseGeometry();
        public HitTestExample()
        {
            InitializeComponent();
        }
        private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            foreach (Rectangle rect in canvas1.Children)
            {
                rect.Fill = Brushes.Red;
            }

            Point pt = e.GetPosition(canvas1);
            hitArea = new EllipseGeometry(pt, 1.0, 1.0);
            hitList.Clear();
            VisualTreeHelper.HitTest(canvas1, null,new HitTestResultCallback(HitTestCallback),new GeometryHitTestParameters(hitArea));
            if (hitList.Count > 0)
            {
                foreach (Rectangle rect in hitList)
                {
                    rect.Fill = Brushes.Blue;
                }
                Console.WriteLine("You hit " + hitList.Count.ToString() + " rectangles.");
            }
        }
        public HitTestResultBehavior HitTestCallback(HitTestResult result)
        {
            IntersectionDetail intersectionDetail = ((GeometryHitTestResult)result).IntersectionDetail;
            switch (intersectionDetail)
            {
                case IntersectionDetail.FullyContains:
                    hitList.Add((Rectangle)result.VisualHit);
                    return HitTestResultBehavior.Continue;
                case IntersectionDetail.Intersects:
                    return HitTestResultBehavior.Continue;
                case IntersectionDetail.FullyInside:
                    return HitTestResultBehavior.Continue;
                default:
                    return HitTestResultBehavior.Stop;
            }
        }
    }
}

   
    
     


Replease mouse with Mouse.Capture(null)


   
  
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MouseInput" Height="300" Width="300">
    <Grid>
      <Ellipse Fill="Blue" x:Name="myEllipse" />
    </Grid>
</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;
using System.Diagnostics;


namespace WpfApplication1
{
    public partial class Window1 : System.Windows.Window
    {
        public Window1()
        {
            InitializeComponent();

           myEllipse.MouseUp += myEllipse_MouseUp;

        }

        void myEllipse_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Mouse.Capture(null);
        }


    }
}

   
    
     


Point Hit Test


   
  
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  xmlns:d="http://schemas.microsoft.com/expression/interactivedesigner/2006"
  mc:Ignorable="d"
  x:Class="InputExamples.PointHitTest" 
  Width="640" Height="480">

  <Window.Resources>
    <Storyboard x:Key="OnLoaded"/>
  </Window.Resources>

  <Window.Triggers>
    <EventTrigger RoutedEvent="FrameworkElement.Loaded">
      <BeginStoryboard x:Name="OnLoaded_BeginStoryboard" Storyboard="{DynamicResource OnLoaded}"/>
    </EventTrigger>
  </Window.Triggers>
    <StackPanel>   
  <Rectangle Fill="Red"  Width="200" Height="200" x:Name="RectangleArea"/>
  <Label d:LayoutOverrides="Height"  Width="Auto" Height="28" x:Name="HitLabel" Content="Label"/>
    </StackPanel>
</Window>
//File:Window.xaml.cs

using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;

namespace InputExamples
{
  public partial class PointHitTest
  {
    private string hitStatus;
    
    public PointHitTest()
    {
      this.InitializeComponent();
    }
    
    protected override void OnInitialized(EventArgs e)
    {
      base.OnInitialized(e);
      
      CompositionTarget.Rendering += this.CompositionTarget_Rendering;
    }

    private void CompositionTarget_Rendering(object sender, EventArgs e)
    {
         Point position = Mouse.GetPosition(RectangleArea);
          
      hitStatus = "no hit";
      
      VisualTreeHelper.HitTest(RectangleArea,null,
              new HitTestResultCallback(HitTestResultHandler),
              new PointHitTestParameters(position)
      );
      this.HitLabel.Content = String.Format("Result of the hit test: {0}", hitStatus);
    }
    
    public HitTestResultBehavior HitTestResultHandler(HitTestResult result)
    {
      PointHitTestResult hitResult = (PointHitTestResult)result;
      
      hitStatus = String.Format("{0} was hit at this point: {1}", 
        ((FrameworkElement)hitResult.VisualHit).Name, 
        hitResult.PointHit.ToString()
      );
         return HitTestResultBehavior.Continue;
    }
  }
}

   
    
     


Get mouse position with Mouse.GetPosition


   
  

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MouseInput" Height="300" Width="300">
    <Grid>
      <Ellipse Fill="Blue" x:Name="myEllipse" />
    </Grid>
</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;
using System.Diagnostics;


namespace WpfApplication1
{
    public partial class Window1 : System.Windows.Window
    {
        public Window1()
        {
            InitializeComponent();

           myEllipse.MouseMove += myEllipse_MouseMove;

        }
        void myEllipse_MouseMove(object sender, MouseEventArgs e)
        {
            Debug.WriteLine(Mouse.GetPosition(myEllipse));
        }


    }
}

   
    
     


Capture Mouse Ellipse


   
  

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MouseInput" Height="300" Width="300">
    <Grid>
      <Ellipse Fill="Blue" x:Name="myEllipse" />
    </Grid>
</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;
using System.Diagnostics;


namespace WpfApplication1
{
    public partial class Window1 : System.Windows.Window
    {
        public Window1()
        {
            InitializeComponent();

            myEllipse.MouseDown += myEllipse_MouseDown;

        }

        void myEllipse_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Mouse.Capture(myEllipse);
        }


    }
}

   
    
     


Use Mouse.Capture to let a Control capture an event


   
  



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