Absolutely centered content inside a WPF layout panel Absolutely centered content inside a WPF layout panel wpf wpf

Absolutely centered content inside a WPF layout panel


<Window x:Class="WpfApplication1.Window1"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    Title="Window1" Height="300" Width="300">    <Grid VerticalAlignment="Center">        <StackPanel HorizontalAlignment="Center">            <TextBlock>First</TextBlock>            <TextBlock>and the second</TextBlock>        </StackPanel>    </Grid></Window>

You could also write your own Panel subclass that does this automatically.


Fixed it as i asked it!

What i needed to do was place a StackPanel inside a ContentControl and set the StackPanels VerticalAlignment to Center. Seems obvious now!