画线和编程移动 [英] Draw line and move it programmatically

查看:109
本文介绍了画线和编程移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要画上一个WPF网格线。

I want to draw a line on a WPF Grid.

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



据绘制没有问题。但现在我想给四个按钮添加到网格(上,下,左,右)。所以,当我按下按钮的行应该在我选择的方向移动的一个。

It is drawn without problems. But now i want to add four buttons to the grid (up, down, left, right). So when i press one of the buttons the line should move in the direction i choose.

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

这是我想使用这个功能,但它不工作。那么它是如何可以移动这一行?

This was the function i want to use for this, but it doesn't work. So how it is possible to move this line?

在结束我像一个老terminal3270一个GUI,这些GUI有一个插入符。该行应该像一个十字线(和帮助,看看那里的插入符号实际上是)

In end I have a gui like an old terminal3270 and these gui has a caret. the lines should be like a crosshair (and help to see where the caret actually is)

推荐答案

首先,我不会操纵坐标作为其主要用于限定线的形状。
其次,我不会用帆布,但渲染转型,而不是,因为它可能对GPU而不是CPU,这使得动画更加平滑运行。

Firstly, I would not manipulate coordinates as their are primarily for defining shape of the line. Secondly, I wouldn't use Canvas, but Render Transformation instead, as it potentially runs on GPU instead of CPU which makes animation smoother.

所以我会做这样的事情:

So I would do something like this:

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>

代码背后:

   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>

重要!如果您在XAML定义仿射变换矩阵和动画吧,在某些时候WPF将取代该矩阵的实例,如果你指的是在你的代码,矩阵你可能不能够操纵的对象。

Important! If you define Affine transformation matrix in XAML and animate it, at some point WPF will substitute the instance of that matrix and if you are refering to that matrix in your code you might not be able to manipulate an object.

旁注:我宁愿创建XAML所有UI元素(Blend是对于一个伟大的工具),然后将使用从代码中引用它们背后

这篇关于画线和编程移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆