以编程方式更改椭圆 C# 的边距

Programmatically change margin of an Ellipse C#

我正在编写一个时间管理小工具,它可以显示我什么时候可以离开以完成合同规定的工作时间。 该工具如下所示:Tool Screenshot 我想让它可以调整大小,所以我尝试根据 window 高度和宽度设置所有形式。这看起来像

     public void resizeElements()
    {
        clockBody.Height = this.Height / (300/248);
        clockBody.Width = this.Width / (300/248);
        double marginClock = Math.Round((this.Height - clockBody.Height) / 2);
        clockBody.Margin = new Thickness(marginClock);
        recHr.Height = this.Height / (300 / 50);
        recHr.Width = this.Width / (300/6);
        recMin.Height = this.Height / (300/80);
        recMin.Width = this.Width / (300 / 4);
        recSec.Height = this.Height / (300/118);
        recSec.Width = this.Width / (300/4);
    }

正如您在图片中看到的那样,钟体没有 Window 大,所以我尝试将它的边距设置为 Math.Round((this.Height - clockBody.Height) / 2); 但这会引发 System.ArgumentException 调用:

'System.ArgumentException' in WindowsBase.dll ("'Auto,Auto,Auto,Auto' is not a valid value for property 'Margin'.")

如果我像 clockBody.Margin = new Thickness(26); ((300-248)/2) 这样编辑代码,我可以 运行 应用程序。但这不是动态的。有任何想法吗?

尝试使用 ActualWidth and ActualHeigth 属性而不是 WidthHeight