使用C#和WPF在代码中绘制线条 [英] Drawing lines in code using C# and WPF

查看:84
本文介绍了使用C#和WPF在代码中绘制线条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to create a digital clock display using 7 segment displays. I can draw lines in XAML by using code like this:

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

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

But when I try to do it in code(from MainWindow()), it doesn't work:

        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条线,然后根据需要为不同的数字切换它们的可见性.我敢肯定,这可以通过很多方法完成,但是为什么我不能在这样的代码中画线呢?

The idea is I can draw 7 lines, then toggle their visibility as required for different numbers. I'm sure this can be done many ways, but why can't I draw lines in code like this?

推荐答案

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

Is that your entire drawing code? If so, you need to add the line object to your surface. If you're using a Canvas for example:

myCanvas.Children.Add(line);

这会将您的行添加到画布.此刻,您只是在创建线,而不是将其放置在任何地方.

This will add your line to your canvas. At the moment, you're just creating the line but not putting it anywhere.

您可以在此MSDN页面.

这篇关于使用C#和WPF在代码中绘制线条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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