用代码更改矩形边距
本文关键字:代码 | 更新日期: 2023-09-27 18:27:29
我想通过按下键来移动矩形,但目前我收到一个错误,上面写着:
中发生类型为"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);
}
尝试
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);
}
}