Как я могу сосредоточить всплывающее окно на моем окне в XAML? - PullRequest
2 голосов
/ 20 мая 2019

Я хочу иметь всплывающие уведомления по центру в моем окне (Высота = "400" Ширина = "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

Есть предложения?Спасибо;)

1 Ответ

3 голосов
/ 20 мая 2019

Это должно центрировать Popup в родительском окне:

<Popup ...
    Placement="Center" 
    PlacementTarget="{Binding RelativeSource={RelativeSource AncestorType=Window}}" />
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...