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;
}

しかし、これは.Content = _canvas; ウィンドウのコンテンツをオーバーライドすると、ビジュアル エディターと MainWindow.xaml を使用できなくなります。それらを組み合わせる方法は?

4

1 に答える 1

3

ウィンドウに複数の子を追加する場合は、layout containerlikestackpanel, gridなどをウィンドウに追加してから、これらのコンテナーに子を追加する必要があります。

この例では、ウィンドウの一部にこれを持たせたい場合は、ウィンドウに をCanvas追加してから、ContentControlそれを に設定する必要があります。ContentCanvas

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

およびコードビハインドで:

this.myContent.Content = _canvas;

このようにして、コンテンツコントロールのみが更新されます

于 2013-09-23T05:11:09.227 に答える