title: Keep popup within window bounds
body: <p>I'm trying to display a popup under a button. </p>
  
  <p>This is what it looks like now : </p>
  
  <p><img src="https://i.stack.imgur.com/b3xWN.png" alt="enter image description here">
  But I want it to stay within the window borders, something like this (example in paint)</p>
  
  <p><img src="https://i.stack.imgur.com/BwXVB.png" alt="enter image description here"></p>
  
  <p>This is my top bar with the popup declared at the bottom:</p>
  
  <pre><code>&lt;Grid&gt;
      &lt;Grid.RowDefinitions&gt;
          &lt;RowDefinition Height="55" /&gt;
          &lt;RowDefinition Height="35" /&gt;
      &lt;/Grid.RowDefinitions&gt;
  
      &lt;Grid x:Name="AdminContainer" Grid.Row="0"&gt;
          &lt;Grid.ColumnDefinitions&gt;
              &lt;ColumnDefinition Width="4*" /&gt;
              &lt;ColumnDefinition Width="10" /&gt;
              &lt;ColumnDefinition Width="Auto" /&gt;
          &lt;/Grid.ColumnDefinitions&gt;
  
          &lt;Border Background="Transparent"&gt;
              &lt;Viewbox StretchDirection="DownOnly" Stretch="Uniform" Height="35" HorizontalAlignment="Left" Margin="10,0,0,0"&gt;
                  &lt;ContentControl Content="{StaticResource LynxLogo}" Margin="0,5" /&gt;
              &lt;/Viewbox&gt;
          &lt;/Border&gt;
  
          &lt;ToggleButton x:Name="AdminOptionsToggleButton" 
                        Grid.Column="2" 
                        VerticalAlignment="Center"
                        HorizontalAlignment="Stretch"
                        IsChecked="{Binding mainViewModel.OptionsPopupOpen, Mode=TwoWay}"
                        Style="{StaticResource EmptyToggleButtonStyle}"
                        Cursor="Hand"&gt;
  
              &lt;Grid x:Name="AdminPanel" HorizontalAlignment="Right" Height="45"
                    VerticalAlignment="Stretch" Margin="0,0,2,0" &gt;
                  &lt;Grid.Style&gt;
                      &lt;Style&gt;
                          &lt;Setter Property="Grid.Background" Value="Transparent"/&gt;
                          &lt;Style.Triggers&gt;
                              &lt;DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=ToggleButton}, Path=IsMouseOver}" Value="True"&gt;
                                  &lt;Setter Property="Grid.Background" Value="#FF404040"/&gt;
                              &lt;/DataTrigger&gt;
                              &lt;DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=ToggleButton}, Path=IsChecked}" Value="True"&gt;
                                  &lt;Setter Property="Grid.Background" Value="#FF353535"/&gt;
                              &lt;/DataTrigger&gt;
                          &lt;/Style.Triggers&gt;
                      &lt;/Style&gt;
                  &lt;/Grid.Style&gt;
  
                  &lt;Grid.ColumnDefinitions&gt;
                      &lt;ColumnDefinition Width="*" /&gt;
                      &lt;ColumnDefinition Width="45" /&gt;
                  &lt;/Grid.ColumnDefinitions&gt;
  
  
                  &lt;Border Grid.Column="0" Padding="20,0,0,0"&gt;
                      &lt;Grid&gt;
                          &lt;Grid.RowDefinitions&gt;
                              &lt;RowDefinition Height="*" /&gt;
                              &lt;RowDefinition Height="*" /&gt;
                          &lt;/Grid.RowDefinitions&gt;
  
                          &lt;!--Name--&gt;
                          &lt;TextBlock Grid.Row="0" Text="{Binding adminViewModel.Name}"
                                     Style="{StaticResource AdminTextBlockStyle}" VerticalAlignment="Bottom"
                                     FontSize="16" /&gt;
                          &lt;!--Status--&gt;
                          &lt;TextBlock Grid.Row="1" Text="{Binding adminViewModel.StatusDescription}"
                                     Style="{StaticResource AdminTextBlockStyle}" VerticalAlignment="Bottom"
                                     FontSize="13" Margin="0,0,0,1" /&gt;
                      &lt;/Grid&gt;
                  &lt;/Border&gt;
  
                  &lt;Border Grid.Column="1" HorizontalAlignment="Right" Margin="3"&gt;
                      &lt;Viewbox StretchDirection="DownOnly"&gt;
                          &lt;Image x:Name="ProfilePicture" Source="/Image/ProfilePictures/profile_placeholder.png" /&gt;
                      &lt;/Viewbox&gt;
                  &lt;/Border&gt;
              &lt;/Grid&gt;
          &lt;/ToggleButton&gt;
      &lt;/Grid&gt;
  
      &lt;views:OptionPopup x:Name="OptionsMenu" PlacementTarget="{Binding ElementName=AdminPanel}"
                         Placement="Bottom" 
                         MinWidth="{Binding ActualWidth, ElementName=AdminPanel}"
                         MaxWidth="{Binding ActualWidth, ElementName=AdminContainer}" /&gt;
  </code></pre>