开发者

Drawing lines in code using C# and WPF

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" />

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;

The idea is I can draw 7 lines, then togg开发者_JS百科le 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?


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.

You can find more information on drawing in WPF on this MSDN page.


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;

    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜