Using drag and drop event handlers you can do like that:
<Canvas x:Name="gamearea" AllowDrop="True" Width="400" Height="400"
DragOver="Gamearea_OnDragOver" DragEnter="Gamearea_OnDragEnter">
<Canvas.Background>
<ImageBrush ImageSource="/Assets/chessboard.jpg" />
</Canvas.Background>
<Image Source="/Assets/queen.png" CanDrag="True" Width="50" Height="50"
Stretch="Fill" DragStarting="Image_OnDragStarting"
Canvas.Top="100" Canvas.Left="150"/>
<Image Source="/Assets/queen.png" CanDrag="True" Width="50" Height="50"
Stretch="Fill" DragStarting="Image_OnDragStarting"
Canvas.Top="250" Canvas.Left="50"/>
<Image Source="/Assets/queen.png" CanDrag="True" Width="50" Height="50"
Stretch="Fill" DragStarting="Image_OnDragStarting"
Canvas.Top="300" Canvas.Left="250"/>
</Canvas>
private void Image_OnDragStarting(UIElement sender, DragStartingEventArgs args)
{
args.Data.Properties.Add("chessman", sender);
}
private void Gamearea_OnDragEnter(object sender, DragEventArgs e)
{
e.DragUIOverride.IsCaptionVisible = false;
e.AcceptedOperation = DataPackageOperation.None;
}
private void Gamearea_OnDragOver(object sender, DragEventArgs e)
{
var canvas = (Canvas) sender;
DataPackageView dataPackageView = e.DataView;
var chessman = (Image) dataPackageView.Properties["chessman"];
(int x, int y)[] others = gamearea.Children.Where(c => c != chessman).Select(i => (GetCoordinates(canvas, i))).ToArray();
(int x, int y) chessmanPoint = GetCoordinates(canvas, e.GetPosition(gamearea));
foreach ((int x, int y) other in others)
{
if (other.x == chessmanPoint.x || other.y == chessmanPoint.y || Math.Abs(other.x - chessmanPoint.x) == Math.Abs(other.y - chessmanPoint.y))
{
e.AcceptedOperation = DataPackageOperation.None;
return;
}
e.AcceptedOperation = DataPackageOperation.Move;
}
}
private static (int x, int y) GetCoordinates(Canvas canvas, Point point)
{
return GetCoordinates(canvas, point.X, point.Y);
}
private static (int x, int y) GetCoordinates(Canvas canvas, UIElement uiElement)
{
double top = Canvas.GetTop(uiElement);
double left = Canvas.GetLeft(uiElement);
return GetCoordinates(canvas, left, top);
}
private static (int x, int y) GetCoordinates(Canvas canvas, double left, double top)
{
double cellWidth = canvas.ActualWidth / 8;
double cellHeight = canvas.ActualHeight / 8;
var x = (int) Math.Floor(left / cellWidth);
var y = (int) Math.Floor(top / cellHeight);
return (x, y);
}