WPF Control Centering

I have a window where I add a new one UserControl(with the image), I just want to center the control in the middle of the screen (both vertically and horizontally). I can only get vertical work. I am going to exchange content in DockPanelon my CodeBehind and want to show this startup screen before I start to run my slideshow user interface, this means that the content is installed from CodeBehind.

My Window:

<Window x:Class="GreenWebPlayerWPF.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="512" Width="853" WindowStyle="None" WindowState="Maximized" WindowStartupLocation="CenterScreen">
    <DockPanel Width="Auto" Height="Auto" Name="TransitionContainer" Background="Black" Margin="0" LastChildFill="True"></DockPanel>
</Window>

My UserControl:

<UserControl x:Class="GreenWebPlayerWPF.FrontPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <DockPanel Background="Black">
        <Image Name="image1" Stretch="None" Source="/GreenWebPlayerWPF;component/gw.png" />
    </DockPanel>
</UserControl>

Please note that I use full / full screen.

+6
source share
3 answers

Use Grid:

  <Grid>  
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="*"/>
      <ColumnDefinition Width="Auto"/>
      <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
      <RowDefinition Height="*"/>
      <RowDefinition Height="Auto"/>
      <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <!-- Replace with your UserControl -->
    <Button Content="Foo" Grid.Column="1" Grid.Row="1"/>
  </Grid>

DockPanel ( DockPanel), . , , , ​​ .

+12

. StackPanel , HorizontalAlignment , . , . , , , .

, , , StackPanels , , , . , - . , , .

<StackPanel Background="Bisque" Orientation="Vertical" Width="300" Height="300" >
    <StackPanel HorizontalAlignment="Center"  Orientation="Horizontal"
                  Height="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=StackPanel}, Path=ActualHeight}">
        <StackPanel VerticalAlignment="Center" Width="200" Height="60" Background="Blue">
        </StackPanel>
    </StackPanel>
</StackPanel>
+3

, , :

<Grid Background="Aqua" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">

    <StackPanel Background="WhiteSmoke" VerticalAlignment="Center" HorizontalAlignment="Center" Height="350" Width="600">
        <TextBox Name="StationDescriptionTextBlock" />
        <Button Margin="10" Name="ProceedButton">Proceed</Button>
    </StackPanel>

</Grid>
0

Source: https://habr.com/ru/post/1715738/


All Articles