Reputation: 1
I have a WPF application and I'm trying to center a popup inside the main window and have it fill the window, but without overflowing the borders. However, the popup appears offset for some reason: Popup offset. As can be seen in the picture, the popup is low and to the right. Below is my code for the popup and the size converters:
XAML:
<Popup Name="MenuPopup" Closed="MenuPopup_Closing" Placement="Center" PlacementTarget="{Binding ElementName=Window1}" IsOpen="False"
AllowsTransparency="True" StaysOpen="False" Grid.RowSpan="2" PopupAnimation="Fade">
<Grid Name="MenuGrid" MouseDown="Popups_MouseDown" Height="{Binding ActualHeight, ElementName=Window1, Converter={StaticResource windowHeightConverter}}" Width="{Binding ActualWidth,ElementName=Window1,Converter={StaticResource windowWidthConverter}}">
<controls:MenuView />
<Grid.Background>
<SolidColorBrush Color="{DynamicResource GreyBackgroundColor}" Opacity="{DynamicResource BackgroundOpacity}"/>
</Grid.Background>
</Grid>
</Popup>
Converters:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
double _height = SystemParameters.WindowCaptionHeight + SystemParameters.ResizeFrameHorizontalBorderHeight;
return ((double)value - _height);
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
double _width = SystemParameters.ResizeFrameVerticalBorderWidth*2;
return ((double)value - _width);
}
I can fix the issue by using horizontal and vertical offsets to force the popup into the position, but it seems like a dirty hack that might not work for different windows themes or OS's.
Am I missing something simple, or does anybody have any ideas?
Upvotes: 0
Views: 2201
Reputation: 1141
So I was able to get the popup to work Exactly as you want in code:
Popup MenuPopup = (Popup)this.Resources["popup"];
double dTitleHeight = SystemParameters.WindowCaptionHeight + SystemParameters.ResizeFrameHorizontalBorderHeight;
double dVerticalBorderWidth = SystemParameters.ResizeFrameVerticalBorderWidth;
MenuPopup.Placement = System.Windows.Controls.Primitives.PlacementMode.Absolute;
MenuPopup.HorizontalOffset = this.PointToScreen(new Point(0, 0)).X - dVerticalBorderWidth *2 ;
MenuPopup.VerticalOffset = this.PointToScreen(new Point(0, 0)).Y - dTitleHeight - dVerticalBorderWidth;
MenuPopup.Height = this.ActualHeight;
MenuPopup.Width = this.ActualWidth ;
MenuPopup.IsOpen = true;
XAML:
<Window.Resources>
<local:WindowWidthConverter x:Key="windowWidthConverter" />
<local:WindowHeightConverter x:Key="windowHeightConverter" />
<Popup x:Key="popup" IsOpen="False" AllowsTransparency="True" StaysOpen="False" PopupAnimation="Fade">
<Grid Name="MenuGrid" >
<controls:MenuView />
<Grid.Background>
<SolidColorBrush Color="{DynamicResource GreyBackgroundColor}" Opacity="{DynamicResource BackgroundOpacity}"/>
</Grid.Background>
</Grid>
</Popup>
</Window.Resources>
Upvotes: 1