2013-09-23 177 views
0

我正在學習WPF。比如我想畫圓圈。我可以這樣:WPF內容覆蓋

private const ushort _operatorRadius = 50; 
public MainWindow() 
     { 
      var _canvas = new Canvas(); 
      var _operatorEllipse = new Ellipse(); 
      SolidColorBrush mySolidColorBrush = new SolidColorBrush(); 

      // Describes the brush's color using RGB values. 
      // Each value has a range of 0-255. 
      mySolidColorBrush.Color = Color.FromArgb(255, 255, 255, 0); 
      _operatorEllipse.Fill = mySolidColorBrush; 
      _operatorEllipse.StrokeThickness = 1; 
      _operatorEllipse.Stroke = Brushes.Black; 

      // Set the width and height of the Ellipse. 
      _operatorEllipse.Width = _operatorRadius; 
      _operatorEllipse.Height = _operatorRadius; 
      _canvas.Children.Add(curOperElips); 
      this.Content = _canvas; 
} 

但this.Content = _canvas;將覆蓋Windows的內容,然後我不能使用可視化編輯器和MainWindow.xaml。如何結合他們?

+1

不太清楚你問什麼;您可以使用XAML代替代碼進行繪製;雖然您的窗口只能有一個內容,但您可以將該內容製作爲網格,堆疊面板,DockPanel等容器;然後他們可以託管許多物品或更多的容器。 – andrew

回答

3

如果你想添加多個孩子到你的窗口,那麼你應該添加一個layout containerstackpanel, grid等,然後添加孩子到這些容器。

在這個例子中,如果你希望你的窗口的某些部分有這種Canvas,那麼你應該在你的窗口中添加ContentControl然後將其ContentCanvas

<StackPanel> 
    <ContentControl x:Name="myContent"/> 
    </StackPanel> 
</Window> 

,並在後面的代碼:

this.myContent.Content = _canvas; 

這樣,只有你的ContentControl中會更新