2012-03-14 15 views
7

Quiero trazar una línea en una cuadrícula WPF.Dibujar línea y moverla programáticamente

private void InitializeTestline() 
{ 
    testline = new Line(); 
    grid.Children.Add(testline); 
    testline.X1 = 0; 
    testline.X2 = 1; 
    testline.Y1 = 0; 
    testline.Y2 = 1; 
    testline.HorizontalAlignment = System.Windows.HorizontalAlignment.Left; 
    testline.VerticalAlignment = System.Windows.VerticalAlignment.Top; 
    testline.Stroke = Brushes.Red; 
    testline.Stretch = Stretch.Fill; 
    testline.StrokeThickness = 2; 
    testline.Visibility = System.Windows.Visibility.Visible; 
} 

Se dibuja sin problemas. Pero ahora quiero agregar cuatro botones a la grilla (arriba, abajo, izquierda, derecha). Entonces, cuando presione uno de los botones, la línea se moverá en la dirección que elija.

private void MoveUp_Click(object sender, RoutedEventArgs e) 
{ 
    this.testline.Y1 += move; 
    this.testline.Y2 += move; 
} 

Esta es la función que quiero usar para esto, pero no funciona. Entonces, ¿cómo es posible mover esta línea?

En el extremo tengo una interfaz gráfica de usuario como un viejo terminal3270 y estos gui tiene un caret. las líneas deberían ser como una cruz (y ayudar a ver dónde está realmente la interpolación)

+0

Lo siento, erróneamente pensé que estaba hablando de WPF DataGrid – Flot2011

+2

Intente comentar //testline.Stretch = Stretch.Fill; – Klaus78

+0

¡Gracias, funcionará bien ahora! ¿Sabes por qué no funciona con Stretch? – Pippl

Respuesta

3

En primer lugar, no manipularía las coordenadas ya que son principalmente para definir la forma de la línea. En segundo lugar, no usaría Canvas, sino Render Transformation, ya que potencialmente se ejecuta en GPU en lugar de CPU, lo que hace que la animación sea más fluida.

Así que me gustaría hacer algo como esto:

XAML:

<Grid x:Name="LayoutRoot"> 

    <Line x:Name="crosshair" 
      HorizontalAlignment="Left" 
      VerticalAlignment="Top" 
      Stroke="Red" 
      X1="0" 
      X2="0" 
      Y1="10" 
      Y2="0" /> 

    <Button Width="50" 
      Height="50" 
      HorizontalAlignment="Right" 
      Click="MoveRight" 
      Content="&gt;" /> 
</Grid> 

código subyacente:

public partial class MainWindow : Window 
    { 
     private int moveRightDist = 0; 

     public MainWindow() 
     { 
      InitializeComponent(); 
     } 

     private void MoveRight(object sender, RoutedEventArgs e) 
     { 
      this.moveRightDist++; 
      crosshair.RenderTransform = new TranslateTransform(this.moveRightDist, 0); 
     } 
    } 

<Grid x:Name="LayoutRoot"> 

    <Line x:Name="crosshair" 
      HorizontalAlignment="Left" 
      VerticalAlignment="Top" 
      Stroke="Red" 
      X1="0" 
      X2="0" 
      Y1="10" 
      Y2="0" /> 

    <Button Width="50" 
      Height="50" 
      HorizontalAlignment="Right" 
      Click="MoveRight" 
      Content="&gt;" /> 
</Grid> 

Importante! Si define la matriz de transformación Affine en XAML y la anima, en algún momento WPF sustituirá la instancia de esa matriz y, si hace referencia a esa matriz en su código, es posible que no pueda manipular un objeto.

Lado Nota: Prefiero crear todos los elementos de la interfaz de usuario en XAML (la mezcla es una gran herramienta para eso) y luego utilizaría hacer referencia a ellos desde el código de atrás.

-1

que estaba haciendo esta misma cosa básica sólo en un laberinto, y esto es lo que yo haría

private void Move_Up Click(object sender, RoutedEventArgs e) 
{ 
    Point testlinelocation; 
    testlinelocation = testline.Y1; 
    testlinelocation.Offset(someX, someY); 
    testlinelocation = testline.Y1; 
} 

Esto debería funcionar, funcionó para mí, lo mejor de la suerte Esto está en winforms

Cuestiones relacionadas