【问题标题】:Programmatically change margin of an Ellipse C#以编程方式更改椭圆 C# 的边距
【发布时间】:2017-01-31 21:50:02
【问题描述】:

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

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

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

WindowsBase.dll 中的“System.ArgumentException”(“'Auto,Auto,Auto,Auto' 不是属性 'Margin' 的有效值。”)

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

【问题讨论】:

  • 你知道当你除以 (300/248) 时,实际上是除以 1 吗?这就是整数除法的工作原理。所以要小心。

标签: c# wpf visual-studio-2015 .net-4.0


【解决方案1】:

尝试使用ActualWidthActualHeigth 属性而不是WidthHeight

【讨论】:

  • 哇,谢谢。我现在可以运行应用程序,但边距设置不正确。您可以在 Pictrue 上看到的 Clockring 是 windowclass 中的一个椭圆,clockbody 是 usercontrol 中的一个椭圆。椭圆现在居中,但不是边距 26 :O
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-17
  • 2011-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-27
相关资源
最近更新 更多