如何在 XAML 中将弹出窗口置于我的 window 中心?
How can i centred a popup on my window in XAML?
我想在我的 window 上设置一个居中的弹出通知(Height="400" Width="500")。请参阅下面的 xaml 代码。是否可以仅使用 xaml(没有后面的代码)将其居中?
</Grid>
<TextBlock Text="TEXT" Grid.Row="1" Grid.Column="0" Grid.RowSpan="2" Grid.ColumnSpan="3" FontSize="200" FontWeight="Bold" HorizontalAlignment="Center"/>
<Grid Grid.Row="1" Grid.Column="1">
<Popup x:Name="PopupName" Grid.ColumnSpan="2" IsOpen="{Binding ElementName=ToggleButton,Path=IsChecked}" StaysOpen="False" Placement="Absolute"
AllowsTransparency="True" PopupAnimation="None" Focusable="False" HorizontalAlignment="Center" VerticalAlignment="Center">
<Grid Height="400" Width="500">
<Rectangle Fill="Silver" Opacity="0.5" Panel.ZIndex="1" Height="400" Width="500"/>
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center" Panel.ZIndex="2" Height="150" Width="150">
<ListBox Height="Auto" HorizontalAlignment="Center" >
<Label Content="Red" Background="Red" Margin="5" VerticalAlignment="Top"/>
<Label Content="Green" Background="Green" Margin="5" VerticalAlignment="Center"/>
<Label Content="Yellow" Background="Yellow" Margin="5" VerticalAlignment="Bottom"/>
</ListBox>
</StackPanel>
<ToggleButton Panel.ZIndex="2" Background="Coral" Foreground="White" FontWeight="Bold" Name ="Button" Width="80" Height="30" HorizontalAlignment="Center" Margin="5,170,5,5" Content="Close" Click="Button_OnClick"/>
</Grid>
</Popup>
</Grid>
另一种选择是将 Placement ="AbsolutePoint" 与 Horizon-/Verticaloffset 一起使用,但它看起来有点奇怪 :D
有什么建议吗?谢谢 ;)
这应该使 Popup
在父 window 中居中:
<Popup ...
Placement="Center"
PlacementTarget="{Binding RelativeSource={RelativeSource AncestorType=Window}}" />
我想在我的 window 上设置一个居中的弹出通知(Height="400" Width="500")。请参阅下面的 xaml 代码。是否可以仅使用 xaml(没有后面的代码)将其居中?
</Grid>
<TextBlock Text="TEXT" Grid.Row="1" Grid.Column="0" Grid.RowSpan="2" Grid.ColumnSpan="3" FontSize="200" FontWeight="Bold" HorizontalAlignment="Center"/>
<Grid Grid.Row="1" Grid.Column="1">
<Popup x:Name="PopupName" Grid.ColumnSpan="2" IsOpen="{Binding ElementName=ToggleButton,Path=IsChecked}" StaysOpen="False" Placement="Absolute"
AllowsTransparency="True" PopupAnimation="None" Focusable="False" HorizontalAlignment="Center" VerticalAlignment="Center">
<Grid Height="400" Width="500">
<Rectangle Fill="Silver" Opacity="0.5" Panel.ZIndex="1" Height="400" Width="500"/>
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center" Panel.ZIndex="2" Height="150" Width="150">
<ListBox Height="Auto" HorizontalAlignment="Center" >
<Label Content="Red" Background="Red" Margin="5" VerticalAlignment="Top"/>
<Label Content="Green" Background="Green" Margin="5" VerticalAlignment="Center"/>
<Label Content="Yellow" Background="Yellow" Margin="5" VerticalAlignment="Bottom"/>
</ListBox>
</StackPanel>
<ToggleButton Panel.ZIndex="2" Background="Coral" Foreground="White" FontWeight="Bold" Name ="Button" Width="80" Height="30" HorizontalAlignment="Center" Margin="5,170,5,5" Content="Close" Click="Button_OnClick"/>
</Grid>
</Popup>
</Grid>
另一种选择是将 Placement ="AbsolutePoint" 与 Horizon-/Verticaloffset 一起使用,但它看起来有点奇怪 :D
有什么建议吗?谢谢 ;)
这应该使 Popup
在父 window 中居中:
<Popup ...
Placement="Center"
PlacementTarget="{Binding RelativeSource={RelativeSource AncestorType=Window}}" />