I have a canvas in the middle of my application with controls around it. I have a socket that recieves Points and saves them in a list.
I draw small 4x4 rectangles on th
I think more nifty solutions are available through binding the Canvas.Left and Canvas.Top attached properties to a ObservableCollection<Point
>, but as you asked for an old fashioned WinForms Style solution here you have something that does what I think you need (My apologies for writing this in C#):
XAML:
<Window x:Class="MovingPointsSpike.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="650" Width="525"
>
<StackPanel>
<Border BorderThickness="1" BorderBrush="Gray">
<Canvas Name="PointCanvas" Width="500" Height="500"/>
</Border>
<Button Name="Move" Click="Move_Click">Move Random Point</Button>
<Button Name="Add" Click="Add_Click">Add Point</Button>
<Button Name="Remove" Click="Remove_Click">Remove Random Point</Button>
</StackPanel>
</Window>
Code behind:
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
namespace MovingPointsSpike
{
public partial class MainWindow : Window
{
private List<Point> m_Points;
private Random m_Random;
public MainWindow()
{
InitializeComponent();
m_Points=new List<Point>();
m_Random=new Random();
}
private void Move_Click(object sender, RoutedEventArgs e)
{
Rectangle rectangle;
Point newPoint;
int index = GetRandomIndex();
newPoint = GetRandomPoint();
rectangle =(Rectangle)PointCanvas.Children[index];
if (index == -1) return;
Canvas.SetTop(rectangle, newPoint.Y);
Canvas.SetLeft(rectangle, newPoint.X);
}
private void Add_Click(object sender, RoutedEventArgs e)
{
Point newPoint;
Rectangle rectangle;
newPoint = GetRandomPoint();
rectangle = new Rectangle {Width = 4, Height = 4, Fill = Brushes.Red};
m_Points.Add(newPoint);
PointCanvas.Children.Add(rectangle);
Canvas.SetTop(rectangle,newPoint.Y);
Canvas.SetLeft(rectangle,newPoint.X);
}
private Point GetRandomPoint()
{
int x;
int y;
x = m_Random.Next(10, 490);
y = m_Random.Next(10, 490);
return new Point(x,y);
}
private void Remove_Click(object sender, RoutedEventArgs e)
{
int index = GetRandomIndex();
if (index==-1)return;
PointCanvas.Children.RemoveAt(index);
m_Points.RemoveAt(index);
}
private int GetRandomIndex()
{
int index;
if (m_Points.Count==0) return -1;
index = m_Random.Next(m_Points.Count - 1);
return index;
}
}
}
Use Rendering event
CompositionTarget.Rendering += UpdateRectangles;
...
protected void UpdateRectangles(object sender, EventArgs e)
{
// here some stuff
Canvas.SetLeft(rectangle, location);
}
Consider using CacheMode="BitmapCache" for them.