Detect whether the mouse button is pressed or released using the MouseButtonState property.
<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.vb
Imports System
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Media
Imports System.Windows.Input
Namespace WpfApplication1
Public Partial Class Window1
Inherits Window
Public Sub New()
InitializeComponent()
End Sub
Private Sub HandleButtonDown(sender As Object, e As MouseButtonEventArgs)
Dim sourceStackPanel As StackPanel = TryCast(e.Source, StackPanel)
If e.ButtonState = MouseButtonState.Pressed Then
sourceStackPanel.Width = 200
sourceStackPanel.Height = 200
ElseIf e.ButtonState = MouseButtonState.Released Then
sourceStackPanel.Width = 100
sourceStackPanel.Height = 100
End If
End Sub
End Class
End Namespace
Related examples in the same category