【问题标题】:Changing rectangle margin with code使用代码更改矩形边距
【发布时间】:2014-10-23 12:12:41
【问题描述】:

我想通过按下键来移动我的矩形,但目前我收到错误消息:

“System.ArgumentException”类型的未处理异常发生在 WindowsBase.dll

附加信息:“Auto,Auto,0,0”不是 属性“边距”。

我的代码如下:

private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            double x = Canvas.GetLeft(rect);
            double y = Canvas.GetTop(rect);

            if (e.Key == Key.D)
            {
                rect.Margin = new Thickness(x+5, y, 0, 0);
            }
            else if (e.Key == Key.A)
            {
                rect.Margin = new Thickness(x-5, y, 0, 0);
            }
            Thread.Sleep(100);
        }

【问题讨论】:

  • 你在矩形上设置Canvas.LeftCanvas.Top吗?

标签: c# wpf


【解决方案1】:

试试

private void Window_KeyDown(object sender, KeyEventArgs e)
    {
        double x = rect.Margin.Left;
        double y = rect.Margin.Top;
        if (e.Key == Key.D)
        {
            rect.Margin = new Thickness(x+5, y, 0, 0);
        }
        else if (e.Key == Key.A)
        {
            rect.Margin = new Thickness(x-5, y, 0, 0);
        }
        Thread.Sleep(100);
    }
}

【讨论】:

  • 嗨尼克,欢迎来到 SO!你能提供更多细节吗?这如何解决问题?
猜你喜欢
  • 2016-10-10
  • 2014-11-24
  • 1970-01-01
  • 1970-01-01
  • 2012-07-10
  • 1970-01-01
  • 2017-01-21
  • 2016-12-26
  • 2017-12-06
相关资源
最近更新 更多