在每行中绘制矩形,直到Canvas中没有空格 [英] Draw rectangles in each row until no space in Canvas
本文介绍了在每行中绘制矩形,直到Canvas中没有空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好专家
我正在研究WPF windows应用程序,我试图通过vb.net创建Rectangle
我能够只做一行。
我需要在每一行重复此操作,直到画布中没有位置。
这是我的XAML
Hello Experts
I am working on WPF windows application where I am trying to create Rectangle through vb.net
I am able to do it for just one row.
I would need to repeat this on Each row until there is no place in the Canvas.
Here is my XAML
<Grid>
<ScrollViewer CanContentScroll="True" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="visible">
<Canvas Height="700" Width="1200" Name="front_canvas" Grid.Row="3" Grid.Column="2" >
<Canvas.RenderTransform>
<ScaleTransform ScaleX="1" ScaleY="1" />
</Canvas.RenderTransform>
</Canvas>
</scrollviewer>
</Grid>
这是我的Vb.net
Here is my Vb.net
Private _top As Double = 0
Protected Sub DrawRect()
'init rectangles
'front_canvas.Width - 1 Step 1100
For i As Integer = 1 To front_canvas.Width - 1 Step 100
Dim rect As New Rectangle()
rect.StrokeThickness = 1
rect.Stroke = System.Windows.Media.Brushes.Black
rect.Width = 50
rect.Height = 50
rect.Name = "box" + i.ToString()
If Not i / 2 = 0 Then
Canvas.SetLeft(rect, i)
Canvas.SetTop(rect, Top)
_top += rect.Height
End If
_rectangles.Add(rect)
Next
For Each rect In _rectangles
front_canvas.Children.Add(rect)
Next
End Sub
我正在寻找类似下面的东西
I am looking for something like below
o o o o
o o o o
o o o o
or
oo oo oo oo
oo oo oo oo
oo oo oo oo
上面的所有o都是我的情况下的矩形。
使用当前代码我的应用程序显示如下
All the o's in the above are Rectangles in my case.
With the current code my application shows like below
o o o o o o o o
感谢一些帮助
Would appreciate some help in this
推荐答案
问题似乎与你的表述一样明显。你提到只有一行,并且会立即尝试查看你将矩形移动到哪一行。很明显,因为你使用Canvas.SetLeft
,但从不Canvas.SetTop
。显然,对于每一行,您应该使用不同的顶部坐标。将调用添加到Canvas.SetTop
:
Canvas.SetTop方法(System.Windows.Controls) [ ^ ]。
这就是全部。
现在,请看我对这个问题的评论。除此之外:如果你只是开始编写整洁的代码,很多你的小错误就会出现。看,现在你的代码基于立即常量,如1110,10,5,100和50.显然它们是相互关联的。你不应该硬编码,比方说,移位,因为它们是根据矩形的数量和大小计算出来的。你应该计算一切。大多数直接常量应该成为具有计算值的变量,其他应该变为显式声明的常量,从不重复或方法参数。只有这样你才能维护你的代码并减少错误。
-SA
The problem seems to be as obvious as your formulation. You mention "just one row" and immediately one would try to see where you shift the rectangle for a row. And it becomes obvious, because you useCanvas.SetLeft
, but neverCanvas.SetTop
. Apparently, for every row you should use different top coordinate. Add the call toCanvas.SetTop
:
Canvas.SetTop Method (System.Windows.Controls)[^].
That's all.
Now, please see my comment to the question. In addition to that: many your minor bugs such as this one will go if you simply start to write neat code. Look, right now your code is based on immediate constants like 1110, 10, 5, 100 and 50. It's apparent that they are interrelated. You should not hard-code, say, shifts, because they are calculated out of number of rectangles and the size. You should calculate everything. Most of your immediate constants should become variables with calculated values, other should become either explicitly declared constants, never repeated, or method parameters. Only then you will be able to maintain your code and will have less bugs.
—SA
这篇关于在每行中绘制矩形,直到Canvas中没有空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文