[英]Why are my polygons being drawn in the wrong location?
I am practicing C# basic WPF/XAML drawing for an assignment and right off the bat I cannot figure out why my polygons are being drawn in the wrong place. 我正在练习C#基本WPF / XAML绘图以进行分配,并且一无所获,我无法弄清楚为什么在错误的位置绘制了多边形。
My window is of 1280x720 fixed, non-resizeable. 我的窗口是固定的1280x720,不可调整大小。 I am trying to programmatically create my polygons by:
我正在尝试通过以下方式以编程方式创建多边形:
Creating points in the coordinates I want them to be: ` 我希望在坐标中创建点:
` `
I tried breaking down the code to something really explicit to see if I could figure out where the issue is, so this is what I have: 我尝试将代码分解为非常明确的内容,以查看是否可以找出问题所在,所以这就是我所拥有的:
private void CreatePolygons()
{
List<Point> PointList = new List<Point>
{
new Point(MainUI.Height / 2, MainUI.Width / 2),
new Point(0, 0),
new Point(0, MainUI.Height),
new Point(MainUI.Width, MainUI.Height),
new Point(MainUI.Width, 0)
};
Polygon p1 = new Polygon();
Polygon p2 = new Polygon();
Polygon p3 = new Polygon();
Polygon p4 = new Polygon();
p1.Points.Add(PointList[0]);
p1.Points.Add(PointList[1]);
p1.Points.Add(PointList[2]);
p2.Points.Add(PointList[0]);
p2.Points.Add(PointList[2]);
p2.Points.Add(PointList[3]);
p3.Points.Add(PointList[0]);
p3.Points.Add(PointList[3]);
p3.Points.Add(PointList[4]);
p4.Points.Add(PointList[0]);
p4.Points.Add(PointList[4]);
p4.Points.Add(PointList[1]);
p1.Stroke = System.Windows.Media.Brushes.LightSkyBlue;
p2.Stroke = System.Windows.Media.Brushes.LightSkyBlue;
p3.Stroke = System.Windows.Media.Brushes.LightSkyBlue;
p4.Stroke = System.Windows.Media.Brushes.LightSkyBlue;
p1.StrokeThickness = 1;
p2.StrokeThickness = 1;
p3.StrokeThickness = 1;
p4.StrokeThickness = 1;
MainGrid.Children.Add(p1);
MainGrid.Children.Add(p2);
MainGrid.Children.Add(p3);
MainGrid.Children.Add(p4);
}
The end result is a completely misplaced grid and I can't understand what the coordinates it ended up creating refer to: 最终结果是网格完全放错了位置,我不明白它最终创建的坐标是指什么:
What am I missing? 我想念什么?
You have accidentally swapped the Width
and Height
in the first point: 您不小心在第一点交换了
Width
和Height
:
new Point(MainUI.Height / 2, MainUI.Width / 2),
Should be: 应该:
new Point(MainUI.Width / 2, MainUI.Height / 2),
Further, assuming MainUI
is the app window itself, the points will still be a bit off, because the Height
of the window includes its title bar height . 此外,假设
MainUI
本身就是应用程序窗口,则这些点仍然会有点偏离,因为窗口的Height
包括其标题栏height 。 You should better use MainGrid.ActualWidth
and MainGrid.ActualHeight
: 您最好使用
MainGrid.ActualWidth
和MainGrid.ActualHeight
:
List<Point> PointList = new List<Point>
{
new Point(MainGrid.ActualWidth / 2, MainGrid.ActualHeight / 2),
new Point(0, 0),
new Point(0, MainGrid.ActualHeight),
new Point(MainGrid.ActualWidth, MainGrid.ActualHeight),
new Point(MainGrid.ActualWidth, 0)
};
Besides that you have confused Width and Height of the first point, I'd suggest not to create UI elements like Polygons in code behind. 除了混淆了第一点的Width和Height外,我建议不要在后面的代码中创建UI元素,例如Polygons。 Better use an ItemsControl like this:
最好像这样使用ItemsControl:
<Grid SizeChanged="MainUISizeChanged">
<ItemsControl x:Name="polygons">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Polygon Stroke="LightSkyBlue" StrokeThickness="1"
Points="{Binding}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
and assign its ItemsSource property to a collection of PointCollections, eg whenever the size of your MainUI element changes: 并将其ItemsSource属性分配给PointCollections的集合,例如,每当MainUI元素的大小更改时:
private void MainUISizeChanged(object sender, SizeChangedEventArgs e)
{
var points = new List<Point>
{
new Point(e.NewSize.Width / 2, e.NewSize.Height / 2),
new Point(0, 0),
new Point(0, e.NewSize.Height),
new Point(e.NewSize.Width, e.NewSize.Height),
new Point(e.NewSize.Width, 0)
};
polygons.ItemsSource = new List<PointCollection>
{
new PointCollection(new Point[] { points[0], points[1], points[2] }),
new PointCollection(new Point[] { points[0], points[2], points[3] }),
new PointCollection(new Point[] { points[0], points[3], points[4] }),
new PointCollection(new Point[] { points[0], points[4], points[1] }),
};
}
As an alternative to all the Polygon point calcuations, you may use this simple Path element, which produces the same output and stretches automatically: 作为所有多边形点计算的替代方法,您可以使用以下简单的Path元素,该元素会产生相同的输出并自动拉伸:
<Grid>
<Path Stretch="Fill" Stroke="LightSkyBlue" StrokeThickness="1"
Data="M0,0 L1,0 1,1 0,1Z M0,0 L1,1 M0,1 L1,0"/>
</Grid>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.