在wpf中垂直居中一行

时间:2016-04-13 15:30:47

标签: wpf line

以下代码似乎没有垂直居中;例如在排列线时似乎没有考虑到行程厚度:

<Grid VerticalAlignment="Center" Width="32" Height="32">
    <Line Stretch="None" HorizontalAlignment="Stretch" VerticalAlignment="Center" X2="32"
       Stroke="Black"
       StrokeThickness="{Binding StrokeThickness}" />
</Grid>

缺少什么?

1 个答案:

答案 0 :(得分:1)

我认为这可能与StrokeThickness有关,但未更改DesiredSize元素的Line,但您可以解决此问题:

Grid可以替换为Canvas

<Canvas VerticalAlignment="Center" Width="32" Height="32">
    <Line Stretch="None" X2="32" Y1="16" Y2="16"
          Stroke="Black"
          StrokeThickness="15" />
</Canvas>

或者,如果您需要外部Grid,请将Line包裹在Canvas中:

<Grid VerticalAlignment="Center" Width="32" Height="32" >
    <Canvas VerticalAlignment="Center">
        <Line Stretch="None" X2="32" 
               Stroke="Black"
               StrokeThickness="15" />
    </Canvas>
</Grid>