如何在 WPF 中的特定 x,y 屏幕位置绘制矩形?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12763441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to draw a rectangle in WPF at a specific x,y screen location?
提问by zetar
In C#, WPF I've created a rectangle:
在 C# 中,WPF 我创建了一个矩形:
Rectangle myRgbRectangle = new Rectangle();
myRgbRectangle.Width = 1;
myRgbRectangle.Height = 1;
SolidColorBrush mySolidColorBrush = new SolidColorBrush();
Yes, I really just want it to be 1 pixel by 1 pixel. And I want to change the color based on the variable height like so:
是的,我真的只希望它是 1 x 1 像素。我想根据可变高度更改颜色,如下所示:
mySolidColorBrush.Color = Color.FromArgb(255, 0, 0, (byte)height);
myRgbRectangle.Fill = mySolidColorBrush;
Now, how do I draw at a specific x,y location on the screen? I do have a grid (myGrid) on my MainWindow.xaml.
现在,如何在屏幕上的特定 x,y 位置进行绘制?我的 MainWindow.xaml 上确实有一个网格 (myGrid)。
Thanks!
谢谢!
Here's the pertinent code:
这是相关的代码:
myRgbRectangle.Width = 1;
myRgbRectangle.Height = 1;
SolidColorBrush mySolidColorBrush = new SolidColorBrush();
int height;
for (int i = 0; i < ElevationManager.Instance.heightData.GetLength(0); i++)
for (int j = 0; j < ElevationManager.Instance.heightData.GetLength(1); j++)
{
height = ElevationManager.Instance.heightData[i, j] / 100;
// Describes the brush's color using RGB values.
// Each value has a range of 0-255.
mySolidColorBrush.Color = Color.FromArgb(255, 0, 0, (byte)height);
myRgbRectangle.Fill = mySolidColorBrush;
myCanvas.Children.Add(myRgbRectangle);
Canvas.SetTop(myRgbRectangle, j);
Canvas.SetLeft(myRgbRectangle, i);
And it's throwing this error: Specified Visual is already a child of another Visual or the root of a CompositionTarget.
它抛出了这个错误:指定的 Visual 已经是另一个 Visual 的子项或 CompositionTarget 的根。
回答by Mark Hall
You need to use a Canvas
istead of a Grid
. You use coordinates to position elements in a Canvas
versus Column and Row in a Grid
.
您需要使用Canvas
i 而不是Grid
。您可以使用坐标来定位 a 中的元素,Canvas
而不是 中的 Column 和 Row Grid
。
Definition of a Canvas:
画布的定义:
Defines an area within which you can explicitly position child elements by using coordinates that are relative to the Canvas area.
定义一个区域,您可以在该区域内使用相对于 Canvas 区域的坐标明确定位子元素。
You would then use Canvas.SetTop
and Canvas.SetLeft
Properties like this (assuming that your canvas is named myCanvas
):
然后你会像这样使用Canvas.SetTop
和Canvas.SetLeft
属性(假设你的画布被命名myCanvas
):
myCanvas.Children.Add(myRgbRectangle);
Canvas.SetTop(myRgbRectangle, 50);
Canvas.SetLeft(myRgbRectangle, 50);
Edit
编辑
Based on your edit, it is like I said you are adding the same rectangle more than once. You need to be creating it in your For Loop each time you add it. Something like this.
根据您的编辑,就像我说您多次添加相同的矩形一样。每次添加时都需要在 For 循环中创建它。像这样的东西。
for (int i = 0; i < ElevationManager.Instance.heightData.GetLength(0); i++)
for (int j = 0; j < ElevationManager.Instance.heightData.GetLength(1); j++)
{
Rectangle rect = new Rectangle();
rect.Width = 1;
rect.Height = 1;
height = ElevationManager.Instance.heightData[i, j] / 100;
// Describes the brush's color using RGB values.
// Each value has a range of 0-255.
mySolidColorBrush.Color = Color.FromArgb(255, 0, 0, (byte)height);
rect.Fill = mySolidColorBrush;
myCanvas.Children.Add(rect);
Canvas.SetTop(rect, j);
Canvas.SetLeft(rect, i);
}