[英]Center controls in a StackPanel WPF
I have the following stackPanel in WPF that contians a MediaElement and controls: 我在WPF中包含以下包含MediaElement和控件的stackPanel:
<Grid Margin="0,0,0,0">
<StackPanel Background="Black" Margin="0,0,0,0" Height="300" VerticalAlignment="Bottom">
<MediaElement Name="MediaElement" MediaOpened="Element_MediaOpened" Height="270" LoadedBehavior="Manual" UnloadedBehavior="Stop"/>
<StackPanel Background="DarkGray" HorizontalAlignment="Center" Width="464" Orientation="Horizontal">
<!-- Play button. -->
<Image Source="/Images/control_play.png" MouseDown="OnMouseDownPlayMedia" Margin="5" />
<!-- Pause button. -->
<Image Source="/images/control_pause.png" MouseDown="OnMouseDownPauseMedia" Margin="5" />
<Image Source="/images/control_stop.png" MouseDown="OnMouseDownStopMedia" Margin="5" />
<!-- Stop button. -->
<TextBlock Foreground="White" Margin="5" VerticalAlignment="Center"><Run Text="Seek To"/></TextBlock>
<!-- Seek to slider. Ths slider allows you to jump to different parts of the media playback. -->
<Slider x:Name="timelineSlider" Thumb.DragStarted="DragStarted" Thumb.DragCompleted="DragCompleted" Margin="5" ValueChanged="SeekToMediaPosition" Width="70"/>
<TextBlock x:Name="lblProgressStatus" Margin="5"><Run Text="00:00"/></TextBlock>
<TextBlock x:Name="lblSepatator" Margin="5"><Run Text="/"/></TextBlock>
<TextBlock x:Name="lblTotalLength" Margin="5" RenderTransformOrigin="3.607,0.455"><Run Text="00:00"/></TextBlock>
<!-- Play button. -->
<!-- Pause button. -->
</StackPanel>
</StackPanel>
</Grid>
All these controls are appearing to the very left of the stack. 所有这些控件都显示在堆栈的最左侧。
How can I get them to be centered? 如何使他们居中?
I hate StackPanels: they always seem to take way too much effort to position/align as you want. 我讨厌StackPanels:它们似乎总是花费过多的精力来根据需要定位/对齐。 Personally I'd use a grid for getting the layout correct, and a StackPanel just for grouping the items together within that layout cell.
就个人而言,我会使用网格来获取正确的布局,并使用StackPanel仅将项目归入该布局单元中。
<Grid Margin="0,0,0,0">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition = "*"/>
<!-- this column will be centre aligned -->
<ColumnDefinition = "Auto"/>
<ColumnDefinition = "*"/>
</Grid.ColumnDefinitions>
<MediaElement Name="MediaElement" Grid.Column="0" MediaOpened="Element_MediaOpened" Height="270" LoadedBehavior="Manual" UnloadedBehavior="Stop"/>
<StackPanel Background="DarkGray" Grid.Column="1" Width="464" Orientation="Horizontal">
<!-- Play button. -->
<Image Source="/Images/control_play.png" MouseDown="OnMouseDownPlayMedia" Margin="5" />
<!-- Pause button. -->
<Image Source="/images/control_pause.png" MouseDown="OnMouseDownPauseMedia" Margin="5" />
<Image Source="/images/control_stop.png" MouseDown="OnMouseDownStopMedia" Margin="5" />
<!-- Stop button. -->
<TextBlock Foreground="White" Margin="5" VerticalAlignment="Center"><Run Text="Seek To"/></TextBlock>
<!-- Seek to slider. Ths slider allows you to jump to different parts of the media playback. -->
<Slider x:Name="timelineSlider" Thumb.DragStarted="DragStarted" Thumb.DragCompleted="DragCompleted" Margin="5" ValueChanged="SeekToMediaPosition" Width="70"/>
<TextBlock x:Name="lblProgressStatus" Margin="5"><Run Text="00:00"/></TextBlock>
<TextBlock x:Name="lblSepatator" Margin="5"><Run Text="/"/></TextBlock>
<TextBlock x:Name="lblTotalLength" Margin="5" RenderTransformOrigin="3.607,0.455"><Run Text="00:00"/></TextBlock>
<!-- Play button. -->
<!-- Pause button. -->
</StackPanel>
</Grid>
</Grid>
As you have given width to the main stackpanel hence the children aren't coming centered. 由于您已为主堆栈面板指定了宽度,因此子代不会居中。
try this. 尝试这个。
<StackPanel Background="DarkGray" HorizontalAlignment="Center" Width="464" Orientation="Horizontal">
<stackpanel HorizontalAlignment="Center">
<!-- Play button. -->
<Image Source="/Images/control_play.png" MouseDown="OnMouseDownPlayMedia" Margin="5" />
<!-- Pause button. -->
<Image Source="/images/control_pause.png" MouseDown="OnMouseDownPauseMedia" Margin="5" />
<Image Source="/images/control_stop.png" MouseDown="OnMouseDownStopMedia" Margin="5" />
<!-- Stop button. -->
<TextBlock Foreground="White" Margin="5" VerticalAlignment="Center"><Run Text="Seek To"/></TextBlock>
<!-- Seek to slider. Ths slider allows you to jump to different parts of the media playback. -->
<Slider x:Name="timelineSlider" Thumb.DragStarted="DragStarted" Thumb.DragCompleted="DragCompleted" Margin="5" ValueChanged="SeekToMediaPosition" Width="70"/>
<TextBlock x:Name="lblProgressStatus" Margin="5"><Run Text="00:00"/></TextBlock>
<TextBlock x:Name="lblSepatator" Margin="5"><Run Text="/"/></TextBlock>
<TextBlock x:Name="lblTotalLength" Margin="5" RenderTransformOrigin="3.607,0.455"><Run Text="00:00"/></TextBlock>
<!-- Play button. -->
<!-- Pause button. -->
</StackPanel>
</StackPanel>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.