2013-12-20 100 views
0
public void CreateALine() 
{ 
    // Create a Line 
    Line redLine = new Line(); 
    redLine.X1 = 50; 
    redLine.Y1 = 50; 
    redLine.X2 = 200; 
    redLine.Y2 = 200; 

    // Create a red Brush 
    SolidColorBrush redBrush = new SolidColorBrush(); 
    redBrush.Color = Colors.Red; 

    // Set Line's width and color 
    redLine.StrokeThickness = 4; 
    redLine.Stroke = redBrush; 

    // Add line to the Grid. 
    LayoutRoot.Children.Add(redLine); 
} 

我從here 代碼,我需要畫線。 但是在教程中有一些對象LayoutRoot沒有在我的代碼中定義我應該怎麼做?如何畫線WPF

+0

佈局根目錄只是可能在XAML標記中添加的元素名稱。在WPF中,您可以直接訪問這些內容。轉到您的XAML文件並創建一個網格並添加名稱LayoutRoot,或者使用不同的名稱 –

回答

2

它表示您的XAML文件中有一個panel name。它可以是grid or any other panel type

<Window> 
    <Grid x:Name="LayoutRoot"> 
    .... 
    </Grid> 
</Window> 
+0

Ty))x是什麼? – user2950593

+0

'x'是給任何UI組件命名的XAML語法。當你創建WPF項目時,你可以在窗口頂部看到這個聲明 - 'xmlns:x =「http://schemas.microsoft.com/winfx/2006/xaml」'。 –