Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VerticalAlignment="Stretch" for label inside canvas doesn't work

How to use VerticalAlignment="Stretch" with a Label inside a Canvas? I'm trying to center the text "Cancel" in the button as in the code below. To use fixed height and width for the label isn't a desired option.

<Button Name="buttonCancel" Width="80" Height="40" IsCancel="True" Padding="0" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
    <Canvas>
        <Label Canvas.Top="0" Canvas.Left="0" Padding="0" FontSize="10">Esc</Label>
        <Label VerticalContentAlignment="Center" HorizontalContentAlignment="Center" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">Cancel</Label>
    </Canvas>
</Button>
like image 311
Marlos Avatar asked Jan 19 '23 09:01

Marlos


1 Answers

Use a binding to the Canvas's ActualWidth:

<Canvas>
    <Label Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type Canvas}}, Path=ActualWidth}">...</Label>
</Canvas>

But as mentioned above, if you are interested in dynamic stretching layouts, the Canvas is not the ideal choice of control.

like image 104
Ross Avatar answered Jan 22 '23 09:01

Ross