Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Align label to the middle of dock panel

Tags:

wpf

I have a dock panel, with one label in the middle and another button on the far right.

Because of the button the label cannot align to the middle when the windows is maximized.

WPF:

<DockPanel Height="40" HorizontalAlignment="Stretch" Margin="-1,-2,0,0" Name="dockPanel1" VerticalAlignment="Stretch" Width="Auto" OpacityMask="{x:Null}">
            <Label FontSize="18" Content="Sales" FontWeight="Bold" FontFamily="Arial" Width="883" Height="42" HorizontalAlignment="Center" HorizontalContentAlignment="Center" Foreground="White" DockPanel.Dock="Left" VerticalAlignment="Center" VerticalContentAlignment="Center"></Label>
            <Button FontSize="18" Height="47" Width="123" Name="btnStart" Foreground="White" BorderBrush="{x:Null}" FlowDirection="LeftToRight" HorizontalContentAlignment="Center" FontFamily="Arial Rounded MT" ClickMode="Press" DockPanel.Dock="Right" HorizontalAlignment="Right" VerticalAlignment="Center" Padding="0" Content="Start" BorderThickness="0" Focusable="False">
        </DockPanel>
like image 381
Kev Fixes Avatar asked Oct 30 '12 15:10

Kev Fixes


1 Answers

Use a Grid instead of a DockPanel

Grid's allow objects to be placed on top of each other, so you can position your Label in the middle and the Button on the Right

<Grid>
    <Label HorizontalAlignment="Center" VerticalAlignment="Center" />
    <Button HorizontalAlignment="Right" />
</Grid>

Also if you're new to WPF's Layouts, I'd recommend reading through WPF Layouts: A Quick Visual Start so you know what layouts are available and can pick the best one for your sitaution

like image 102
Rachel Avatar answered Oct 13 '22 16:10

Rachel