2011-05-11 28 views
18

我試圖用7段顯示創建數字時鐘顯示。我可以用這樣的代碼繪製線條XAML:使用C#和WPF在代碼中繪製線

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

但是,當我嘗試做的代碼(從主窗口()),這是行不通的:

 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條線,然後根據需要切換他們的可見性爲不同的數字。我相信這可以通過很多方式完成,但爲什麼我不能像這樣在代碼中畫線?

+0

你是什麼意思不起作用,究竟發生了什麼?沒有?看起來你已經創建了這條線,但你沒有做任何事情。就像創建一個可以做很多工作但從未被調用的方法一樣。 – 2011-05-11 19:29:03

+0

對不起,這不是很清楚。我的意思是這條線沒有畫在屏幕上。 – Jesse 2011-05-11 20:06:53

回答

22

這是你的整個圖紙代碼嗎?如果是這樣,你需要添加line對象到你的表面。如果您使用的是Canvas,例如:

myCanvas.Children.Add(line); 

這會將您的線添加到畫布。目前,你只是創建線路,但沒有放在任何地方。

您可以在this MSDN page的WPF中找到更多關於繪圖的信息。

+0

謝謝!這就是我想念的! – Jesse 2011-05-11 19:38:50

+0

假設我想將Line添加到Image中,我該如何做? – Jonas 2014-07-04 10:52:52

+0

@Jonas - 這可能有助於http://stackoverflow.com/questions/5231086/wpf-c-draw-a-line-onto-existing-bitmap-in-image-control – keyboardP 2014-07-05 12:00:23

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

    } 
+4

這是一個代碼只回答,嘗試解釋它能做什麼 – dotctor 2015-06-10 07:44:07