如何在WPF中的特定x,y屏幕位置绘制矩形? [英] How to draw a rectangle in WPF at a specific x,y screen location?

查看:507
本文介绍了如何在WPF中的特定x,y屏幕位置绘制矩形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,WPF创建了一个矩形:

In C#, WPF I've created a rectangle:

        Rectangle myRgbRectangle = new Rectangle();
        myRgbRectangle.Width = 1;
        myRgbRectangle.Height = 1;
        SolidColorBrush mySolidColorBrush = new SolidColorBrush();

是的,我真的只是希望它是1像素乘1像素。我想根据可变高度来更改颜色,如下所示:

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:

        mySolidColorBrush.Color = Color.FromArgb(255, 0, 0, (byte)height);
        myRgbRectangle.Fill = mySolidColorBrush;

现在,如何在屏幕上特定的x,y位置绘制?我在MainWindow.xaml上确实有一个网格(myGrid)。

Now, how do I draw at a specific x,y location on the screen? I do have a grid (myGrid) on my MainWindow.xaml.

谢谢!

以下是相关代码:

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

并抛出此错误:指定的Visual已经是另一个Visual的子级或CompositionTarget的根。

And it's throwing this error: Specified Visual is already a child of another Visual or the root of a CompositionTarget.

推荐答案

您需要使用 画布 而不是 Grid 。您可以使用坐标将元素放置在画布中,而将列和行放置在 Grid 中。

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.

画布的定义:


定义一个区域,您可以在该区域中通过使用以下坐标来明确定位子元素相对于画布区域。

Defines an area within which you can explicitly position child elements by using coordinates that are relative to the Canvas area.

然后您将使用 Canvas.SetTop Canvas.SetLeft 这样的属性(假定您的画布名为 myCanvas ):

You would then use Canvas.SetTop and Canvas.SetLeft Properties like this (assuming that your canvas is named myCanvas):

 myCanvas.Children.Add(myRgbRectangle);
 Canvas.SetTop(myRgbRectangle, 50);
 Canvas.SetLeft(myRgbRectangle, 50);






编辑

根据您的编辑,就像我说过您要多次添加同一个矩形。每次添加时,都需要在For循环中创建它。

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

这篇关于如何在WPF中的特定x,y屏幕位置绘制矩形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆