0

我正在学习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; 将覆盖窗口的内容,然后我不能使用可视化编辑器和 MainWindow.xaml。如何将它们结合起来?

4

1 回答 1

3

If you want to add multiple children to your window then you should add a layout container like stackpanel, grid etc to it and then add children to these containers.

In this example if you want some section of your window to have this Canvas then you should add a ContentControl in your window and then set its Content to the Canvas

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

and in code behind:

this.myContent.Content = _canvas;

In this way only your contentcontrol will update

于 2013-09-23T05:11:09.227 回答