C# 如何在XAML中将弹出窗口设置为中心?

C# 如何在XAML中将弹出窗口设置为中心?,c#,wpf,xaml,popup,C#,Wpf,Xaml,Popup,我想在我的窗口上有一个居中的弹出通知(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="C

我想在我的窗口上有一个居中的弹出通知(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>

    </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”与水平/垂直偏移一起使用,但这似乎有点奇怪:D

    </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>

有什么建议吗?Thx;)

这将使父窗口中的
弹出窗口居中:

    </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>
<Popup ...
    Placement="Center" 
    PlacementTarget="{Binding RelativeSource={RelativeSource AncestorType=Window}}" />


可能的副本:感谢链接,但是它把弹出窗口的左上角放在屏幕中间,它不在我的窗口中心弹出。我希望它没有代码隐藏。有什么想法吗?这很有帮助。谢谢:)