使用WPF的Viewbox中的多个折线

时间:2013-07-08 15:16:56

标签: c# wpf graphics polyline

我想在WPF中的Polyline中“绘制”几个Textblock和一些LabelViewbox

由于Viewbox仅允许单个子项,因此我尝试将Polyline放入Canvas元素中但不起作用:

XAML:

<Viewbox Stretch="Uniform">
     <Canvas Margin="10">
        <Polyline 
                        Points="{Binding Path=Points2}"
                        Stroke="Green"
                        StrokeThickness="2" >
        </Polyline>
                    <!-- other Polylines, Textblocks etc.  would go here... -->
    </Canvas>
</Viewbox>

使用此代码时正确绘制了Polyline(即我只是删除了Canvas):

<Viewbox Stretch="Uniform">
        <Polyline 
                        Points="{Binding Path=Points2}"
                        Stroke="Green"
                        StrokeThickness="2" >
        </Polyline>
</Viewbox>

我希望可视化一些几何属性,例如像geogebra这样非常简约的计算机几何程序。可选地,某些点应该可以在下一个版本中移动,但这不是必需的。

解决方案:

<Viewbox Stretch="Uniform">
     <Grid>
        <Polyline 
                        Points="{Binding Path=Points2}"
                        Stroke="Green"
                        StrokeThickness="4" >
        </Polyline>
        <Polyline 
                        Points="{Binding Path=Points2}"
                        Stroke="Yellow"
                        StrokeThickness="2" >
        </Polyline>
    </Grid>
</Viewbox>

这将相同的多边形放在彼此的顶部,即在宽绿色折线的顶部显示为黄色。

这个stackoverflow question的答案帮助了我。

2 个答案:

答案 0 :(得分:1)

画布不适用于这样的事情,一旦你将控件放在画布中,你就会忽略所有的布局。您是否可以将多边形线放在网格中并使用边距来定位它们?

<Viewbox Stretch="Uniform">
    <Grid  Margin="10">
        <Polyline 
                    Points="{Binding Path=Points2}"
                    Stroke="Green"
                    StrokeThickness="2" >
        </Polyline>
    </Grid>
</Viewbox>

答案 1 :(得分:0)

您无法看到折线的原因是Canvas的默认HeightWidth为0。

尝试明确设置HeightWidth

<Viewbox x:Name="ViewBox" Stretch="Uniform">
    <Canvas x:Name="chartCanvas" Margin="10" Height="200" Width="300">
        <Polyline 
                Points="{Binding Path=Points2}"
                Stroke="Green"
                StrokeThickness="2">
        </Polyline>
        <!-- other Polylines, Textblocks etc.  would go here... -->
    </Canvas>
</Viewbox>