WPF draw random ellipse and locate via coordinate
//xaml <Window x:Class="WpfApp181.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp181" mc:Ignorable="d" WindowState="Maximized" Title="MainWindow" Height="450" Width="800"> <Grid> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition Height="200"/> </Grid.RowDefinitions> <Canvas x:Name="cvs" Grid.Row="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/> <DataGrid x:Name="dg" Grid.Row="1" SelectionChanged="dg_SelectionChanged" SelectionMode="Single" /> </Grid> </Window> //cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApp181 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { Ellipse markedElp { get; set; } public MainWindow() { InitializeComponent(); markedElp = new Ellipse(); markedElp.Width = 30; markedElp.Height = 30; markedElp.Stroke = new SolidColorBrush(Colors.Red); markedElp.Fill = new SolidColorBrush(Colors.Red); this.Loaded += MainWindow_Loaded; } private void MainWindow_Loaded(object sender, RoutedEventArgs e) { InitElps(); } private void InitElps() { List<Book> booksList = new List<Book>(); int width = (int)cvs.ActualWidth - 50; int height = (int)cvs.ActualHeight - 50; Random rnd = new Random(); for (int i = 0; i < 1000; i++) { int x=rnd.Next(0, width); int y=rnd.Next(0, height); Book bk = new Book(); bk.Id = i + 1; bk.PtX = x; bk.PtY = y; booksList.Add( bk ); Ellipse elp = new Ellipse(); elp.Stroke = new SolidColorBrush(Colors.Cyan); elp.StrokeThickness = 3; elp.Width = 20; elp.Height = 20; elp.Fill = new SolidColorBrush(Colors.Blue); Canvas.SetLeft(elp, x); Canvas.SetTop(elp, y); cvs.Children.Add ( elp ); } dg.ItemsSource= booksList; } private void dg_SelectionChanged(object sender, SelectionChangedEventArgs e) { if(e.AddedItems!=null && e.AddedItems.Count>0) { var selectedBk = e.AddedItems[0] as Book; if(selectedBk != null) { if(cvs.Children.Contains(markedElp)) { cvs.Children.Remove(markedElp); } Canvas.SetLeft(markedElp,selectedBk.PtX); Canvas.SetTop(markedElp,selectedBk.PtY); cvs.Children.Add(markedElp); } } } } public class Book { public int Id { get; set; } public int PtX { get; set; } public int PtY { get; set; } } }