I've had a weird behavior in WPF for awhile now, and I haven't been able to trace down where the problem is. In a nutshell, when I resize the window from the bottom or right, everything works as expected. But if I happen to grab it from the top or left, it stretches the window bu not the window contents. I've tried playing around wirh HorizontalContentAlignment
, HorizontalAlignment
, VerticalContentAlignment
, & VerticalAlignment
to no avail. Anybody have any ideas where the issue lies?
Resize from Left:
Resize from Top:
Resize from Right/Bottom:
Here's the XAML I'm using, with the inner controls removed for brevity:
Window XAML settings:
<Window x:Class="Agent_Template.MainWindow"
Width="{Binding Source={x:Static main:Properties.Settings.Default}, Path=Width, Mode=TwoWay}"
FontFamily="{Binding Source={x:Static main:Properties.Settings.Default}, Path=currentFont, Mode=TwoWay}"
FontSize="{Binding Source={x:Static main:Properties.Settings.Default}, Path=currentFontSize, Mode=TwoWay}"
Foreground="{Binding Source={x:Static main:Properties.Settings.Default}, Path=foregroundColor, Mode=TwoWay}"
LocationChanged="Window_LocationChanged" Tag="parentWindow"
Top="{Binding Source={x:Static main:Properties.Settings.Default}, Path=Top, Mode=TwoWay}"
Topmost="False">
Container XAML Settings:
<DockPanel Name="rvraDockPanel" Background="{Binding ElementName=BackColorPicker, Path=CurrentColor}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Menu Height="Auto" DockPanel.Dock="Top">
<Menu.ItemsPanel>
<ItemsPanelTemplate>
<DockPanel HorizontalAlignment="Stretch" />
</ItemsPanelTemplate>
</Menu.ItemsPanel>
<WrapPanel Name="buttonDock" Grid.Column="0" HorizontalAlignment="Center" DockPanel.Dock="Bottom" Orientation="Horizontal">
<StatusBar Name="bottomStatusBar" Height="28" MinWidth="{Binding ElementName=buttonPanel, Path=ActualWidth}" Background="{Binding ElementName=clearButton, Path=Background}" BorderBrush="White" DockPanel.Dock="Bottom" Focusable="False" FontFamily="{Binding ElementName=fontSelector, Path=SelectedValue}" FontWeight="Bold" Foreground="Blue">
<Grid Width="{Binding ElementName=bottomStatusBar, Path=ActualWidth}" HorizontalAlignment="Center">
<TabControl Name="tabSelection" HorizontalContentAlignment="Center" Background="{Binding ElementName=BackColorPicker, Path=CurrentColor}">
Update: LocationChanged code as requested
private void Window_LocationChanged(object sender, EventArgs e)
{
if (Properties.Settings.Default.windowSnap == true)
{
RealignChild();
}
}
/// <summary>
/// Checks position of any SlideOut windows in relation to main
/// program window and aligns child window next to main window.
/// </summary>
private void RealignChild()
{
foreach (Window win in App.Current.Windows)
{
if (!win.IsFocused && win.Tag.ToString() == "childWindow" && win.Left < this.Left)
{
win.Left = this.Left - win.Width;
}
if (!win.IsFocused && win.Tag.ToString() == "childWindow" && win.Left > this.Left)
{
win.Left = this.Left + this.Width;
}
win.Top = this.Top;
}
}
Turns out this was the problem, as when I removed the XAML part of it the problem was corrected. I do want to keep this method though, as there's a method that depends upon it to keep ChildWindows
of the MainWindow
locked to the edge. I would like to continue using this if possible.