Click here to Skip to main content
15,921,697 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
hi i'm trying to monitor the mouse movement over the application window
so that whenever the mouse moves its coordinates are reflected in two text conrols
is it possible to do it in XAML only (without code)?
Fredrik Bornander 7-Mar-11 11:48am    
Why do you not want to use code to do it?
fjdiewornncalwe 7-Mar-11 14:53pm    
Sounds like a test question. What I think you mean to ask is whether this can be done using only XAML without any code behind.
badge2033 8-Mar-11 2:39am    
i've done it with some c# code
but i want to figure out a way to do it with xaml only

any help would be welcome
Tarun.K.S 8-Mar-11 7:59am    
Man its quite tough to do it only in XAML!
amitkarnik2211 10-Mar-11 7:37am    
Badge if u get the answer pls post it here

Pete is right but if you are really insistant on avoiding code, you could do this. Write a custom class ONCE, and then use that class in XAML. This way your actual UI remains mostly code-behind free. Here's a quickly put together example (you may need to clean and polish this up).

First step is to create a class such as:

public class MousePosition : DependencyObject
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool GetCursorPos(ref NativePoint pt);

    internal struct NativePoint
        public int X;
        public int Y;

    public static Point GetCurrentMousePosition()
        NativePoint nativePoint = new NativePoint();
        GetCursorPos(ref nativePoint);
        return new Point(nativePoint.X, nativePoint.Y);

    private Dispatcher dispatcher;

    Timer timer = new Timer(100);

    public MousePosition()
        dispatcher = Application.Current.MainWindow.Dispatcher;
        timer.Elapsed += timer_Elapsed;

    void timer_Elapsed(object sender, ElapsedEventArgs e)
        Point current = GetCurrentMousePosition();
        this.CurrentPosition = current;

    public Point CurrentPosition
        get { return (Point)GetValue(CurrentPositionProperty); }

            dispatcher.Invoke((Action)(() =>
              SetValue(CurrentPositionProperty, value)));

    public static readonly DependencyProperty CurrentPositionProperty
      = DependencyProperty.Register(
        "CurrentPosition", typeof(Point), typeof(MousePosition));

Now use it in XAML :

  <local:MousePosition x:Key="mousePosition" />

  <StackPanel Orientation="Horizontal">
      <TextBlock>Mouse Position:</TextBlock>
        Text="{Binding Source={StaticResource mousePosition}, Path=CurrentPosition}"
        Width="360" />
Share this answer
Pete O'Hanlon 8-Apr-11 6:05am    
Good answer mate.
There is no way to do this entirely in the XAML. Basically, the method to get the mouse position depends on tracking the mouse move event, and converting the value using the GetPosition method to get the point. Your options are to:

a) Use code in a traditional C# class
b) Use x:Code to embed code in the XAML
c) Use the AvalonLambda[^] converter extension.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900