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
{
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetCursorPos(ref NativePoint pt);
[StructLayout(LayoutKind.Sequential)]
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;
timer.Start();
}
void timer_Elapsed(object sender, ElapsedEventArgs e)
{
Point current = GetCurrentMousePosition();
this.CurrentPosition = current;
}
public Point CurrentPosition
{
get { return (Point)GetValue(CurrentPositionProperty); }
set
{
dispatcher.Invoke((Action)(() =>
SetValue(CurrentPositionProperty, value)));
}
}
public static readonly DependencyProperty CurrentPositionProperty
= DependencyProperty.Register(
"CurrentPosition", typeof(Point), typeof(MousePosition));
}
Now use it in XAML :
<Window.Resources>
<local:MousePosition x:Key="mousePosition" />
</Window.Resources>
<Grid>
<StackPanel Orientation="Horizontal">
<TextBlock>Mouse Position:</TextBlock>
<TextBlock
Text="{Binding Source={StaticResource mousePosition}, Path=CurrentPosition}"
Width="360" />
</StackPanel>
</Grid>