3

我有一条垂直线和一条水平线,当我动态调整画布父级时,我想调整它们的大小。(地标)

我希望水平线始终距画布的左右边界 25 处,距底部边界 13 处。

垂直线也是如此,距顶部和底部边框 25 处,距左边框 13 处。

有简单的解决方案吗?我可以将我的画布更改为另一个控件吗?

4

3 回答 3

7

只需将线条粘贴在画布顶部的网格中即可获得正确的行为

<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>
于 2012-06-25T15:00:17.153 回答
3

我会Converters根据你的andActualHeight来设置你的对象的高度、宽度和位置ActualWidthCanvasLine

为了避免编写一堆单独的转换器,我在我的博客上发布了一个可以用于所有计算的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>

因为这些都是绑定,所以它们会在绑定属性更改时刷新(MyCanvas.ActualHeightMyCanvas.ActualWidth

于 2012-06-25T14:24:17.407 回答
1

在需要设置的情况下使用Grid而不是。CanvasMargin

要使您的线条与边框有空间,请转到“属性”并Margin在“布局区域”中使用以设置空间。对于您的水平线,将VerticalAlignmenttoBottom和 Horizo​​ntalAlignment 设置为Stretch。保证金应25,0,25,13在这种情况下。

对于您的垂直线,将 the设置VerticalAlignment为to 。保证金应该是StretchHorizontalAlignmentLeft13,25,0,25

祝你好运

于 2012-06-25T14:38:07.890 回答