WPF内容覆盖

时间:2013-09-23 05:04:51

标签: c# 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;
}

但是这个。内容= _canvas;将覆盖窗口的内容,然后我不能使用可视化编辑器和MainWindow.xaml。如何将它们结合起来?

1 个答案:

答案 0 :(得分:3)

如果您想在窗口中添加多个子项,则应向其中添加layout container stackpanel, grid等,然后将子项添加到这些容器中。

在此示例中,如果您希望窗口的某些部分包含此Canvas,则应在窗口中添加ContentControl,然后将其Content设置为Canvas }

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

并在代码背后:

this.myContent.Content = _canvas;

这样只有你的contentcontrol才会更新