Hi All, (using v2.4)I've been using a custom cursor for a long time now as part of the standard features in the apps I write. It's simply
Partial Public Class CustomMapCursor
Inherits UserControl
Public Sub New()
InitializeComponent()
End Sub
Private ReadOnly MapProperty As DependencyProperty = DependencyProperty.Register("Map", GetType(FrameworkElement), GetType(CustomMapCursor), New PropertyMetadata(Nothing, AddressOf OnMapPropertyChanged))
Public Property Map() As FrameworkElement
Get
Return DirectCast(GetValue(MapProperty), FrameworkElement)
End Get
Set(ByVal value As FrameworkElement)
SetValue(MapProperty, value)
End Set
End Property
Private Shared Sub OnMapPropertyChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
Dim target As CustomMapCursor = DirectCast(d, CustomMapCursor)
If e.OldValue IsNot Nothing Then
Dim oldMap As FrameworkElement = CType(e.OldValue, FrameworkElement)
RemoveHandler oldMap.MouseMove, AddressOf target.Map_MouseMove
RemoveHandler oldMap.MouseLeave, AddressOf target.Map_MouseLeave
RemoveHandler oldMap.MouseEnter, AddressOf target.Map_MouseEnter
End If
If e.NewValue IsNot Nothing Then
Dim newMap As FrameworkElement = CType(e.NewValue, FrameworkElement)
AddHandler newMap.MouseMove, AddressOf target.Map_MouseMove
AddHandler newMap.MouseLeave, AddressOf target.Map_MouseLeave
AddHandler newMap.MouseEnter, AddressOf target.Map_MouseEnter
End If
End Sub
Private ReadOnly SourceProperty As DependencyProperty = DependencyProperty.Register("Source", GetType(DataTemplate), GetType(CustomMapCursor), New PropertyMetadata(Nothing, AddressOf OnSourceChanged))
Public Property Source() As DataTemplate
Get
Return CType(GetValue(SourceProperty), DataTemplate)
End Get
Set(ByVal value As DataTemplate)
SetValue(SourceProperty, value)
End Set
End Property
Private Overloads Shared Sub OnSourceChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
Dim target As CustomMapCursor = CType(d, CustomMapCursor)
Dim oldSource As DataTemplate = CType(e.OldValue, DataTemplate)
Dim newSource As DataTemplate = target.Source
target.CustomCursor.ContentTemplate = newSource
target.OnSourceChanged(oldSource, newSource)
End Sub
Protected Overridable Overloads Sub OnSourceChanged(ByVal oldSource As DataTemplate, ByVal newSource As DataTemplate)
RaiseEvent CursorChanged(Me, New System.EventArgs)
End Sub
Private Sub MoveTo(ByVal pt As Point)
If Me.Map.Cursor IsNot Cursors.None Then
Me.Map.Cursor = Cursors.None
End If
CustomCursor.SetValue(Canvas.LeftProperty, pt.X)
CustomCursor.SetValue(Canvas.TopProperty, pt.Y)
End Sub
Public Shared Event CursorChanged As System.EventHandler
Private Sub Map_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
MoveTo(e.GetPosition(Map))
End Sub
Private Sub Map_MouseLeave(sender As Object, e As MouseEventArgs)
LayoutRoot.Visibility = Windows.Visibility.Collapsed
End Sub
Private Sub Map_MouseEnter(sender As Object, e As MouseEventArgs)
LayoutRoot.Visibility = Windows.Visibility.Visible
End Sub
End Class
XAML is:
<UserControl x:Class="[ProjectName].CustomMapCursor"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Canvas x:Name="LayoutRoot" IsHitTestVisible="False">
<ContentControl x:Name="CustomCursor"/>
</Canvas>
</UserControl>
It is put in the app like so:
<local:CustomMapCursor x:Name="MyCursor" Map="{Binding ElementName=MyMap}" Source="{StaticResource PanCursor}" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Top"/>
(the Map is alone in Grid.Column 1, so the CustomCursor is placed on top of it)The DataTemplate for the Cursor is
<DataTemplate x:Key="PanCursor">
<Image Width="24" Height="24" Source="/[ProjectName];component/CustomMapCursor/Images/PanColor.png">
<Image.RenderTransform>
<TranslateTransform X="-6" Y="-6"/>
</Image.RenderTransform>
</Image>
</DataTemplate>
It is a simple hand. The problem I'm having now that I have never had before is that the rate of movement slows down greatly as more graphics are added. The are points, with a MarkerSymbol defined in a ResourceDictionary as (Cont. in next post)