2010-09-04 169 views
1

我的应用程序中间有一个画布,并带有控件。 我有一个接收点并将它们保存在列表中的套接字。围绕画布移动一个矩形

我在画布上绘制了小小的4x4矩形,用于列表中的点数。 说theres 4点.. theres 4矩形。

我希望能够在代码改变点时移动矩形。 这可能没有故事板或任何'动画'类?我将如何去做我需要的东西?

我曾尝试:

 'cMap.Children.Remove(r) 

     'Dim nr As Rectangle = New Rectangle() With {.Width = 4, .Height = 4, .Name = "r" & P.Name, .Fill = Brushes.Red} 
     'r.RenderTransform = New TranslateTransform(P.Position.X, P.Position.Y) 

     Canvas.SetTop(cMap.Children(cMap.Children.IndexOf(r)), (512/2) + P.Position.Y) 
     Canvas.SetLeft(cMap.Children(cMap.Children.IndexOf(r)), (512/2) + P.Position.X) 
     'nr.SetValue(Canvas.TopProperty, (512/2) + P.Position.Y) 
     'nr.SetValue(Canvas.LeftProperty, (512/2) + P.Position.X) ' P.Position.X) 
     'cMap.Children.Add(nr) 

所有但没有使矩形移动。 是的,我确信数据正在改变。

非常感谢。

回答

1

我认为通过将Canvas.Left和Canvas.Top附加属性绑定到ObservableCollection>上,可以获得更多漂亮的解决方案,但是当你问一个老式的WinForms Style解决方案时,你有什么能做到我认为你的需要(我在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> 

后面的代码:

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; 
     } 
    } 
} 
+0

看,我已经试过了Canvas.S etTop(矩形,newPoint.Y);它只是不移动它的东西。 – tcables 2010-09-04 22:27:17

+1

啊,看到我做错了的事情是我用一个新点覆盖每个点,而不是更新点...导致线程之间的问题。 – tcables 2010-09-05 00:19:41

0

使用渲染事件

CompositionTarget.Rendering += UpdateRectangles; 

...

protected void UpdateRectangles(object sender, EventArgs e) 
{ 
    // here some stuff 
    Canvas.SetLeft(rectangle, location); 

} 

考虑使用CacheMode参数= “BitmapCache” 他们。