0

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.

Keven M
  • 972
  • 17
  • 47
  • 1
    I just copy pasted your container code and its works ok, the problem must be in Window properties somewhere, try removing the bindings for fixed values to see if you can find the offending property – sa_ddam213 Dec 16 '12 at 08:44
  • 1
    I would suggest starting by removing the binding against `Window.Width`. – Kent Boogaart Dec 16 '12 at 08:54
  • 1
    Can you put up the code for your `LocationChanged` event handler? Try disabling this event handler temporarily and see if it is the culprit. Top/left will trigger position changed, I don't believe sizing bottom or right does. – Steve Py Dec 16 '12 at 12:34
  • @steve-py - LocationChanged() added, can you tell why this is an issue? – Keven M Dec 16 '12 at 19:17
  • Hmm, you wouldn't think capturing the event and changing another window's position would cause such an issue. Frustrating. :) This is a bit of a long-shot but have you tried adding an `this.InvalidateVisual` after performing the re-position inside the `LocationChanged` event? Though if that works you may want to ensure it is only triggered when the size of the form changes. (if possible) – Steve Py Dec 16 '12 at 22:28

1 Answers1

0

Are you using a custom Window Resize method? I know from my own experience, that unless you use the default Windows ResizeMethod (As in WindowStyle = AnythingButNone) resizing from anywhere bar the Bottom/Right will encounter problems (Microsoft's fault, I'm sure a little bit of Googling will verify that).

If you aren't using the default Windows ResizeMethod can you please post your code? Otherwise, I have to assume that the problem lies within either your Windows properties or in one of your base elements that encompasses the whole window.

JosephGarrone
  • 4,081
  • 3
  • 38
  • 61