c# - 在 Canvas 中添加椭圆位置

标签 c# wpf silverlight xaml

我有一个椭圆,我想以编程方式将其放入 Canvas 中。不幸的是,椭圆只绘制在右上角。有帮助吗?

SolidColorBrush newColor = HotSquare(col);
        Color newCol = getCol(col);
        Color newCol2 = getCol2(col);

        Ellipse el1 = new Ellipse();
        el1.Height = 40;
        el1.Width = 40;
        RadialGradientBrush rgb = new RadialGradientBrush();
        GradientStop gsa = new GradientStop();
        el1.StrokeThickness = 3;
        el1.Stroke = newColor;
        el1.StrokeDashArray = new DoubleCollection() { 6, 6 };
        gsa.Color = newCol;
        gsa.Offset = 0;
        rgb.GradientStops.Add(gsa);
        GradientStop gsb = new GradientStop();
        gsb.Color = newCol2;
        gsb.Offset = 1;
        rgb.GradientStops.Add(gsb);
        el1.Fill = rgb;

        Ellipse el2 = new Ellipse();
        el2.Height = 40;
        el2.Width = 40;
        el2.StrokeThickness = 3;
        el2.Stroke = newColor;

        //el2.Fill = new SolidColorBrush(Colors.Transparent);

        Canvas canvas = new Canvas();
        canvas.Children.Add(el1);
        //canvas.Children.Add(el2);
        TextBlock txt = new TextBlock();
        txt.Text = "\n   " + amnt.ToString();
        txt.Foreground = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255));
        txt.Height = 60;
        txt.Width = 60;
        txt.HorizontalAlignment = HorizontalAlignment.Center;
        //txt.VerticalAlignment = VerticalAlignment.Bottom;
        txt.FontSize = 12;
        canvas.Children.Add(txt);
        Border border = new Border();
        border.BorderBrush = newColor;
        border.BorderThickness = new Thickness(2);
        border.Child = canvas;
        LocationRect lr = new LocationRect(North, West, South, East);
        HotspotLayer.AddChild(ele, lr);

最佳答案

您可以调用Canvas.SetTopCanvas.SetLeft在子元素上设置它在 Canvas 中的位置。

Canvas.SetTop(el2, 100);
Canvas.SetLeft(el2, 100);

关于c# - 在 Canvas 中添加椭圆位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3237597/

相关文章:

silverlight - Silverlight 中 Grid.SharedSizeGroup 的解决方法

c# - 更改独占锁定对象时的奇怪行为 - Monitor.Enter(x)

c# - FlowDocumentScrollViewer FontSize 不会改变 FlowDocument 内容的 FontSize

silverlight - 在 XAML 代码中编写条件语句

c# - Windows Phone 7 ListPicker InvalidCastException

wpf - 在 WPF 中呈现非托管视频帧的最有效方法是什么?

c# - 在 Ubuntu 上安装适用于 .NET 的 newtonsoft-json

c# - 文件模式和文件访问和 IOException : The process cannot access the file 'filename' because it is being used by another process

c# - C# 中生成的文本框上的鼠标单击事件

c# - WPF 应用程序 (XAML) 内的 Unity 窗口未调整大小