2012-08-14 16 views
0

我想提請在C# 另一個階級一個矩形進我的主窗口從另一個類添加矩形在C#

<Window x:Class="MyApp.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Name="mainWindow" Height="768" Width="1366" > 
</Window> 

這裏是我試圖繪製一個矩形我的類代碼段主窗口

private UIElement container; 

private Rect rect1 = new Rect(); 

public TestPage(UIElement cont) 
{   
    this.container = cont; 
} 

private void init() 
{ 
    this.container.Children.Add(rect1); 
} 

如何處理?

回答

1

一個窗口只能有一個內容,所以你必須設置Content屬性的Window。

+0

我GOOGLE了,但我不知道應該設置我的mainWindow的Content屬性值。任何提示? – bbklol 2012-08-14 11:05:55

+0

@bbklol:Read this http://stackoverflow.com/questions/7884185/wpf-how-to-dynamically-add-controls-in-dynamically-created-wpf-window – 2012-08-14 11:08:52