【发布时间】:2012-06-25 14:17:27
【问题描述】:
我有一条垂直线和一条水平线,当我动态调整我的画布父级时,我想调整它们的大小。 (地标)
我希望水平线始终距离画布的左右边界 25 距离,距离底部边界 13 距离。
垂直线也是如此,距上下边框 25 处,距左边框 13 处。
有简单的解决方案吗? 我可以将我的画布更改为另一个控件吗?
【问题讨论】:
标签: wpf canvas resize line stretch
我有一条垂直线和一条水平线,当我动态调整我的画布父级时,我想调整它们的大小。 (地标)
我希望水平线始终距离画布的左右边界 25 距离,距离底部边界 13 距离。
垂直线也是如此,距上下边框 25 处,距左边框 13 处。
有简单的解决方案吗? 我可以将我的画布更改为另一个控件吗?
【问题讨论】:
标签: wpf canvas resize line stretch
只需将线条粘贴在画布顶部的网格中即可获得正确的行为
<Grid Width="600" Height="600">
<Canvas Background="LightBlue">
// stuff here
</Canvas>
<Grid>
<Rectangle Fill="Black" Height="1"
Stroke="Black" VerticalAlignment="Bottom" Margin="25,0,25,13"/>
<Rectangle Fill="Black"
HorizontalAlignment="Left" Stroke="Black" Width="1" Margin="13,25,0,25"/>
</Grid>
</Grid>
【讨论】:
Margin="25,0,0,25" 以避免将其绘制在线条之上
我会根据Canvas 的ActualHeight 和ActualWidth 使用Converters 来设置Line 对象的高度、宽度和位置
为避免编写一堆单独的转换器,我在我的博客上发布了一个MathConverter,可用于所有计算。
<Canvas x:Name="MyCanvas">
<!-- Horizontal Line: 25 from each side, and 13 from bottom -->
<!-- May need to adjust the Canvas.Top ConverterParameter based on Line height -->
<Line Height="1"
Canvas.Left="25"
Canvas.Top="{Binding ElementName=MyCanvas, Path=ActualHeight,
Converter={StaticResource MathConverter},
ConverterParameter=@VALUE-14}"
Width="{Binding ElementName=MyCanvas, Path=ActualWidth,
Converter={StaticResource MathConverter},
ConverterParameter=@VALUE-50}" ... />
<!-- Vertical Line: 25 from top and bottom, and 13 from left -->
<Line Canvas.Left="13" Canvas.Top="25"
Height="{Binding ElementName=MyCanvas, Path=ActualHeight,
Converter={StaticResource MathConverter},
ConverterParameter=@VALUE-50}" ... />
</Canvas>
因为这些都是 Bindings,所以只要绑定的属性发生变化,它们就会被刷新(MyCanvas.ActualHeight 和 MyCanvas.ActualWidth)
【讨论】:
如果您需要设置Margin,请使用Grid 而不是Canvas。
要使您的线条与边框有空间,请转到“属性”并在“布局区域”中使用Margin 来设置空间。对于您的水平线,将VerticalAlignment 设置为Bottom,将HorizontalAlignment 设置为Stretch。在这种情况下,保证金应为25,0,25,13。
对于您的垂直线,将VerticalAlignment 设置为Stretch 并将HorizontalAlignment 设置为Left。保证金应该是13,25,0,25
祝你好运
【讨论】: