NavigationService.GetNavigationService

image_pdfimage_print


   
  

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

   
    
     


Navigation Basics

image_pdfimage_print


   
  

<NavigationWindow
    x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="NavigationBasics"
    Height="300"
    Width="300">
</NavigationWindow>
//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.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Navigation;
using System.Windows.Documents;
using System.Diagnostics;


namespace WpfApplication1 {
  public partial class Window1 : NavigationWindow {
    Hyperlink link1 = new Hyperlink();
    Hyperlink link2 = new Hyperlink();
    TextBlock text1 = new TextBlock();
    TextBlock text2 = new TextBlock();

    public Window1() {
      InitializeComponent();
      text1.Inlines.Add(link1);
      text1.VerticalAlignment = VerticalAlignment.Bottom;
      text1.Loaded += text1_Loaded;

      link1.Inlines.Add("Click to see page 2");
      link1.Click += link1_Click;

      text2.Inlines.Add(link2);
      text2.VerticalAlignment = VerticalAlignment.Bottom;
      text2.Loaded += text2_Loaded;

      link2.Inlines.Add("Click to go back to page 1");
      link2.Click += link2_Click;

      this.Navigate(text1);


      Button button1 = new Button();
      Button button2 = new Button();

      button1.Content = "Click to see Button 2";
      button1.Loaded += delegate(object sender, RoutedEventArgs e2) {
          ((NavigationWindow)button1.Parent).Title = "Welcome to button1";
      };
      button1.Click += delegate(object sender, RoutedEventArgs e2) {
          this.Navigate(button2);
      };
      button2.Content = "Click to go back to Button 1";
      button2.Loaded += delegate(object sender, RoutedEventArgs e2) {
          ((NavigationWindow)button2.Parent).Title = "Welcome to button2";
      };
      button2.Click += delegate(object sender, RoutedEventArgs e2) {
         ((NavigationWindow)button2.Parent).GoBack();
      };

      this.Navigate(button1);
    }

    void text1_Loaded(object sender, RoutedEventArgs e) {
      Title = "Welcome to Page 1";
    }

    void text2_Loaded(object sender, RoutedEventArgs e) {
      Title = "Welcome to Page 2";
    }

    void link1_Click(object sender, RoutedEventArgs e) {
      Navigate(text2);
    }

    void link2_Click(object sender, RoutedEventArgs e) {
      NavigationService navService = NavigationService.GetNavigationService((DependencyObject)sender);
      navService.GoBack();
    }
  }
}

   
    
     


Get mouse position with Mouse.GetPosition

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


    }
}

   
    
     


Point Hit Test

image_pdfimage_print


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

   
    
     


Mouse cursor override and clear

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="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();
        }
        private void StartSlowWork()
        {
            Mouse.OverrideCursor = Cursors.AppStarting;
        }

        private void SlowWorkCompleted()
        {
            Mouse.OverrideCursor = null;
        }

        protected override void OnMouseDown(MouseButtonEventArgs e)
        {
            base.OnMouseDown(e);
            if (e.Source != myEllipse)
            {
                StartSlowWork();
            }
        }

        protected override void OnMouseUp(MouseButtonEventArgs e)
        {
            base.OnMouseUp(e);
            SlowWorkCompleted();
        }

    }
}

   
    
     


Check the mouse event source

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="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();
        }
        private void StartSlowWork()
        {
            Mouse.OverrideCursor = Cursors.AppStarting;
        }

        private void SlowWorkCompleted()
        {
            Mouse.OverrideCursor = null;
        }

        protected override void OnMouseDown(MouseButtonEventArgs e)
        {
            base.OnMouseDown(e);
            if (e.Source != myEllipse)
            {
                StartSlowWork();
            }
        }

        protected override void OnMouseUp(MouseButtonEventArgs e)
        {
            base.OnMouseUp(e);
            SlowWorkCompleted();
        }

    }
}

   
    
     


Use the Mouse Wheel action methods that are defined by the IScrollInfo interface

image_pdfimage_print


   
  


<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="ScrollViewer_Methods.Window1"
    Title="ScrollViewer IScrollInfo Sample"
    Loaded="onLoad">
<DockPanel>
<TextBlock DockPanel.Dock="Top" FontSize="20" FontWeight="Bold" Margin="10">IScrollInfo Interface Methods</TextBlock>
<StackPanel DockPanel.Dock="Left" Width="150">
    <Button Click="spMouseWheelDown">MouseWheelDown</Button>
    <Button Click="spMouseWheelUp">MouseWheelUp</Button>
    <Button Click="spMouseWheelLeft">MouseWheelLeft</Button>
    <Button Click="spMouseWheelRight">MouseWheelRight</Button>
</StackPanel>  
<Border BorderBrush="Black" Background="White" BorderThickness="2" Width="500" Height="500">
    <ScrollViewer Name="sv1" CanContentScroll="True" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible">
        <StackPanel Name="sp1">
            <Button>Button 1</Button>
            <Button>Button 5</Button>
            <Rectangle Width="700" Height="500" Fill="Green"/>
            <TextBlock>Rectangle 3</TextBlock>
        </StackPanel> 
    </ScrollViewer>
</Border>
</DockPanel>
</Window>
//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
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)
        {
            ((IScrollInfo)sp1).CanVerticallyScroll = true;
            ((IScrollInfo)sp1).CanHorizontallyScroll = true;
            ((IScrollInfo)sp1).ScrollOwner = sv1;
        }
        private void spMouseWheelDown(object sender, RoutedEventArgs e)
        {
            ((IScrollInfo)sp1).MouseWheelDown();
        }
        private void spMouseWheelUp(object sender, RoutedEventArgs e)
        {
            ((IScrollInfo)sp1).MouseWheelUp();
        }
        private void spMouseWheelLeft(object sender, RoutedEventArgs e)
        {
            ((IScrollInfo)sp1).MouseWheelLeft();
        }
        private void spMouseWheelRight(object sender, RoutedEventArgs e)
        {
            ((IScrollInfo)sp1).MouseWheelRight();
        }
    }
}