使用C#和WPF在代码中绘制线条

Drawing lines in code using C# and WPF

我正在尝试使用7段显示器创建数字时钟显示器。我可以使用以下代码在XAML中绘制线条:

1
<Line Name="line7" Stroke="Black" StrokeThickness="4" X1="10" X2="40" Y1="70" Y2="70" Margin="101,-11,362,250" />

但是当我尝试用代码(从MainWindow()中)执行此操作时,它不起作用:

1
2
3
4
5
6
7
8
9
10
        Line line = new Line();
        Thickness thickness = new Thickness(101,-11,362,250);
        line.Margin = thickness;
        line.Visibility = System.Windows.Visibility.Visible;
        line.StrokeThickness = 4;
        line.Stroke = System.Windows.Media.Brushes.Black;
        line.X1 = 10;
        line.X2 = 40;
        line.Y1 = 70;
        line.Y2 = 70;

我的想法是我可以绘制7条线,然后根据需要为不同的数字切换它们的可见性。我确定这可以通过多种方法完成,但是为什么我不能在这样的代码中画线呢?


这是您的整个绘图代码吗?如果是这样,则需要在表面上添加line对象。例如,如果您使用的是Canvas:

1
myCanvas.Children.Add(line);

这会将您的行添加到画布。目前,您只是在创建线,而没有将其放置在任何地方。

您可以在此MSDN页面上找到有关在WPF中进行绘图的更多信息。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
public class Cls_Barriere
{

    // animazione periferica
    public static void LineAnimation(Line _line,String _colore)
    {
        Storyboard result = new Storyboard();
        Duration duration = new Duration(TimeSpan.FromSeconds(2));

        ColorAnimation animation = new ColorAnimation();
        animation.RepeatBehavior = RepeatBehavior.Forever;
        animation.Duration = duration;
        switch (_colore.ToUpper())
        {
            case"RED":
                animation.From = Colors.Red;
                break;
            case"ORANGE":
                animation.From = Colors.Orange;
                break;
            case"YELLOW":
                animation.From = Colors.Yellow;
                break;
            case"GRAY":
                animation.From = Colors.DarkGray;
                break;
            default:
                animation.From = Colors.Green;
                break;
        }

        animation.To = Colors.Gray;
        Storyboard.SetTarget(animation, _line);
        Storyboard.SetTargetProperty(animation, new PropertyPath("(Line.Stroke).(SolidColorBrush.Color)"));
        result.Children.Add(animation);
        result.Begin();

    }
}
//***************************************************************************  

public partial class MainPage : UserControl
{
    public Line _line;

    public MainPage()
    {
        InitializeComponent();
        Canvas.MouseLeftButtonDown += Canvas_MouseLeftButtonDown;
        Canvas.MouseLeftButtonUp += Canvas_MouseLeftButtonUp;
    }

    void Canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        _line.X2 = e.GetPosition(this.Canvas).X;
        _line.Y2 = e.GetPosition(this.Canvas).Y;
        _line.Loaded += _line_Loaded;
        Canvas.Children.Add(_line);
    }

    void _line_Loaded(object sender, RoutedEventArgs e)
    {
        Cls_Barriere.LineAnimation(sender as Line,"RED");
    }

    void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        _line = new Line();
        _line.Stroke = new SolidColorBrush(Colors.White);
        _line.StrokeThickness = 5;
        _line.StrokeStartLineCap = PenLineCap.Round;

        _line.StrokeEndLineCap = PenLineCap.Round;
        _line.StrokeDashCap = PenLineCap.Round;

        _line.X1 = e.GetPosition(this.Canvas).X;
        _line.Y1= e.GetPosition(this.Canvas).Y;

    }