我已經創建的飛行畫布下面的代碼:定位在主窗口中畫布
public MainWindow()
{
InitializeComponent();
Canvas canvas = new Canvas();
canvas.Height = 300;
canvas.Width = 275;
canvas.Background = Brushes.Blue;
this.AddChild(canvas);
...
}
沒有什麼,除了由Visual Studio生成的樣板主窗口定義相應的XAML(我已刪除VS默認提供的Grid條目):
<Window x:Class="canvas2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Background="Gray">
</Window>
代碼將畫布居中在應用程序主窗口中間。如何通過代碼自己定位畫布?
謝謝,我不知道你必須擁有頂級容器。這個關鍵點幾乎解決了一切。還有一個問題:根是一個WPF概念,還是最高級別的畫布,它被命名爲root [即Canvas root = new Canvas()]。再次感謝。 – Sabuncu 2013-03-16 17:42:41
「root」只是我在XAML中分配給頂層Canvas的名稱:'
謝謝!我需要研究WPF中的「內容」概念。 – Sabuncu 2013-03-16 17:54:45