Detect whether the mouse button is pressed or released using the MouseButtonState property.

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="DetectMouseButtonState" Height="400" Width="400">

  <StackPanel Height="100" Width="100" 
      MouseLeftButtonDown="HandleButtonDown" 
      MouseLeftButtonUp="HandleButtonDown" 
      Background="Red"
      DockPanel.Dock="Left">
    <TextBlock>Click on Me</TextBlock>
  </StackPanel>
</Window>
//File:Window.xaml.cs

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

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        void HandleButtonDown(object sender, MouseButtonEventArgs e)
        {
            StackPanel sourceStackPanel = e.Source as StackPanel;

            if (e.ButtonState == MouseButtonState.Pressed)
            {
                sourceStackPanel.Width = 200;
                sourceStackPanel.Height = 200;
            }else if (e.ButtonState == MouseButtonState.Released)
            {
                sourceStackPanel.Width = 100;
                sourceStackPanel.Height = 100;
            }
        }
    }
}

   
    
     


Handler MouseLeftButtonDown and MouseLeftButtonUp events

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="DetectMouseButtonState" Height="400" Width="400">

  <StackPanel Height="100" Width="100" 
      MouseLeftButtonDown="HandleButtonDown" 
      MouseLeftButtonUp="HandleButtonDown" 
      Background="Red"
      DockPanel.Dock="Left">
    <TextBlock>Click on Me</TextBlock>
  </StackPanel>
</Window>
//File:Window.xaml.cs

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

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        void HandleButtonDown(object sender, MouseButtonEventArgs e)
        {
            StackPanel sourceStackPanel = e.Source as StackPanel;

            if (e.ButtonState == MouseButtonState.Pressed)
            {
                sourceStackPanel.Width = 200;
                sourceStackPanel.Height = 200;
            }else if (e.ButtonState == MouseButtonState.Released)
            {
                sourceStackPanel.Width = 100;
                sourceStackPanel.Height = 100;
            }
        }
    }
}

   
    
     


Mouse Enter and leave a Border

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="mouseEnterLeave" Height="400" Width="500">

  <StackPanel>
    <Border MouseEnter="OnMouseEnterHandler"
            MouseLeave="OnMouseLeaveHandler"
            Name="border1" Margin="10"
            BorderThickness="1"
            BorderBrush="Black"
            VerticalAlignment="Center"
            Width="300" Height="100">
      <Label Margin="10" FontSize="14"
             HorizontalAlignment="Center">Move Cursor Over Me</Label>
    </Border>
  </StackPanel>
</Window>


//File:Window.xaml.cs

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


namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        void OnMouseEnterHandler(object sender, MouseEventArgs e)
        {
            border1.Background = Brushes.Red;
        }
        void OnMouseLeaveHandler(object sender, MouseEventArgs e)
        {
            border1.Background = Brushes.White;
        }
    }
 
}

   
    
     


Make an object follow the mouse pointer as it moves on the screen.

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="mouseMoveWithPointer" Height="400" Width="500">
  <Canvas MouseMove="MouseMoveHandler" Background="Red">
    <Ellipse Name="ellipse" Fill="LightBlue" Width="100" Height="100"/>
  </Canvas>
</Window>
//File:Window.xaml.cs

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


namespace WpfApplication1
{
    public partial class Window1 : Window
    {

        public Window1()
        {
            InitializeComponent();
        }
        private void MouseMoveHandler(object sender, MouseEventArgs e)
        {
            // Get the x and y coordinates of the mouse pointer.
            Point position = e.GetPosition(this);
            double pX = position.X;
            double pY = position.Y;

            ellipse.Width = pX;
            ellipse.Height = pY;
        }
    }
}

   
    
     


Use Mouse.AddPreviewMouseDownHandler(myEllipse, PreviewMouseDownEllipse);

image_pdfimage_print


   
  

<Window x:Class="WpfApplication1.AttachedEvents"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Event Examples" Height="300" Width="300">
  <Grid HorizontalAlignment="Center" VerticalAlignment="Center">
    <Button>
      <Grid>
        <Grid.ColumnDefinitions>
          <ColumnDefinition />
          <ColumnDefinition />
        </Grid.ColumnDefinitions>

          <Ellipse x:Name="myEllipse" Canvas.Left="1" Canvas.Top="1" Width="16" Height="16" Fill="Yellow" Stroke="Black" />

          <Ellipse Canvas.Left="4.5" Canvas.Top="5" Width="2.5" Height="3" Fill="Black" />
          <Ellipse Canvas.Left="11" Canvas.Top="5" Width="2.5" Height="3" Fill="Black" />
          <Path Data="M 5,10 A 3,3 0 0 0 13,10" Stroke="Black" />
      
        <TextBlock Grid.Column="1">Click!</TextBlock>
      </Grid>
    </Button>
  </Grid>
</Window>

//File:Window.xaml.cs

using System;
using System.Windows;
using System.Diagnostics;
using System.Windows.Shapes;
using System.Windows.Input;

namespace WpfApplication1
{
    public partial class AttachedEvents : System.Windows.Window
    {

        public AttachedEvents()
        {
            InitializeComponent();

            Mouse.AddPreviewMouseDownHandler(myEllipse, PreviewMouseDownEllipse);
            Mouse.AddMouseDownHandler(myEllipse, MouseDownEllipse);

        }

        void PreviewMouseDownEllipse(object sender, RoutedEventArgs e)
        { Debug.WriteLine("PreviewMouseDownEllipse"); }

        void MouseDownEllipse(object sender, RoutedEventArgs e)
        { Debug.WriteLine("MouseDownEllipse"); }

    }
}

   
    
     


Use Mouse.AddMouseDownHandler(myEllipse, MouseDownEllipse);

image_pdfimage_print


   
  

<Window x:Class="WpfApplication1.AttachedEvents"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Event Examples" Height="300" Width="300">
  <Grid HorizontalAlignment="Center" VerticalAlignment="Center">
    <Button>
      <Grid>
        <Grid.ColumnDefinitions>
          <ColumnDefinition />
          <ColumnDefinition />
        </Grid.ColumnDefinitions>
          <Ellipse x:Name="myEllipse" Canvas.Left="1" Canvas.Top="1" Width="16" Height="16" Fill="Yellow" Stroke="Black" />
    
        <TextBlock Grid.Column="1">Click!</TextBlock>
      </Grid>
    </Button>
  </Grid>
</Window>

//File:Window.xaml.cs

using System;
using System.Windows;
using System.Diagnostics;
using System.Windows.Shapes;
using System.Windows.Input;

namespace WpfApplication1
{
    public partial class AttachedEvents : System.Windows.Window
    {

        public AttachedEvents()
        {
            InitializeComponent();

            Mouse.AddPreviewMouseDownHandler(myEllipse, PreviewMouseDownEllipse);
            Mouse.AddMouseDownHandler(myEllipse, MouseDownEllipse);

        }

        void PreviewMouseDownEllipse(object sender, RoutedEventArgs e)
        { Debug.WriteLine("PreviewMouseDownEllipse"); }

        void MouseDownEllipse(object sender, RoutedEventArgs e)
        { Debug.WriteLine("MouseDownEllipse"); }

    }
}

   
    
     


Display a message box and get the message box return value.

image_pdfimage_print


   
  


<Window x:Class="MessageBoxSample.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MessageBoxSample" Height="300" Width="500">
  <Grid>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="Auto"></ColumnDefinition>
      <ColumnDefinition></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
      <RowDefinition></RowDefinition>
      <RowDefinition></RowDefinition>
      <RowDefinition></RowDefinition>
      <RowDefinition></RowDefinition>
      <RowDefinition></RowDefinition>
      <RowDefinition></RowDefinition>
      <RowDefinition></RowDefinition>
      <RowDefinition></RowDefinition>
      <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>

    <Label Grid.Column="0" Grid.Row="0">Associate with Owner Window?</Label>
    <CheckBox Grid.Column="1" Grid.Row="0" Name="ownerCheckBox"></CheckBox>

    <Label Grid.Column="0" Grid.Row="1">messageBoxText:</Label>
    <TextBox Grid.Column="1" Grid.Row="1" Name ="messageBoxText">MessageBoxText</TextBox>

    <Label Grid.Column="0" Grid.Row="2">caption:</Label>
    <TextBox Grid.Column="1" Grid.Row="2" Name="caption">Caption</TextBox>

    <Label Grid.Column="0" Grid.Row="3">button:</Label>
    <ComboBox Grid.Column="1" Grid.Row="3" Name="buttonComboBox">
      <ComboBoxItem IsSelected="True">OK</ComboBoxItem>
      <ComboBoxItem>OKCancel</ComboBoxItem>
      <ComboBoxItem>YesNo</ComboBoxItem>
      <ComboBoxItem>YesNoCancel</ComboBoxItem>
    </ComboBox>

    <Label Grid.Column="0" Grid.Row="4">icon:</Label>
    <ComboBox Grid.Column="1" Grid.Row="4" Name="imageComboBox">
      <ComboBoxItem>Asterisk</ComboBoxItem>
      <ComboBoxItem>Error</ComboBoxItem>
      <ComboBoxItem>Exclamation</ComboBoxItem>
      <ComboBoxItem>Hand</ComboBoxItem>
      <ComboBoxItem>Information</ComboBoxItem>
      <ComboBoxItem IsSelected="True">None</ComboBoxItem>
      <ComboBoxItem>Question</ComboBoxItem>
      <ComboBoxItem>Stop</ComboBoxItem>
      <ComboBoxItem>Warning</ComboBoxItem>
    </ComboBox>

    <Label Grid.Column="0" Grid.Row="5">defaultResult:</Label>
    <ComboBox Grid.Column="1" Grid.Row="5" Name="defaultResultComboBox">
      <ComboBoxItem>Cancel</ComboBoxItem>
      <ComboBoxItem>No</ComboBoxItem>
      <ComboBoxItem IsSelected="True">None</ComboBoxItem>
      <ComboBoxItem>OK</ComboBoxItem>
      <ComboBoxItem>Yes</ComboBoxItem>
    </ComboBox>

    <Label Grid.Column="0" Grid.Row="6">options</Label>
    <ComboBox Grid.Column="1" Grid.Row="6" Name="optionsComboBox">
      <ComboBoxItem>DefaultDesktopOnly</ComboBoxItem>
      <ComboBoxItem IsSelected="True">None</ComboBoxItem>
      <ComboBoxItem>RightAlign</ComboBoxItem>
      <ComboBoxItem>RtlReading</ComboBoxItem>
      <ComboBoxItem>ServiceNotification</ComboBoxItem>
    </ComboBox>

    <Button Grid.Column="1" Grid.Row="7" Name="showMessageBoxButton" Click="showMessageBoxButton_Click">Show MessageBox</Button>

    <StatusBar Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="8" >
      <StatusBarItem>
        <TextBlock Name="resultTextBlock">Ready</TextBlock>
      </StatusBarItem>
    </StatusBar>

  </Grid>

</Window>


//File:Window.xaml.cs
using System;
using System.Windows;

namespace MessageBoxSample
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        void showMessageBoxButton_Click(object sender, RoutedEventArgs e)
        {
            Window owner = ((bool)ownerCheckBox.IsChecked ? this : null);
            string messageBoxText = this.messageBoxText.Text;
            string caption = this.caption.Text;
            MessageBoxButton button = (MessageBoxButton)Enum.Parse(typeof(MessageBoxButton), this.buttonComboBox.Text);
            MessageBoxImage icon = (MessageBoxImage)Enum.Parse(typeof(MessageBoxImage), this.imageComboBox.Text);
            MessageBoxResult defaultResult = (MessageBoxResult)Enum.Parse(typeof(MessageBoxResult), this.defaultResultComboBox.Text);
            MessageBoxOptions options = (MessageBoxOptions)Enum.Parse(typeof(MessageBoxOptions), this.optionsComboBox.Text);

            MessageBoxResult result;
            if (owner == null)
            {
                result = MessageBox.Show(messageBoxText, caption, button, icon, defaultResult, options);
            }
            else
            {
                result = MessageBox.Show(owner, messageBoxText, caption, button, icon, defaultResult, options);
            }
            
            resultTextBlock.Text = "Result = " + result.ToString();
        }
    }
}