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.

4

1 回答 1

0

您是否使用自定义窗口调整大小方法?根据我自己的经验,我知道,除非您使用默认的 Windows ResizeMethod(如 中WindowStyle = AnythingButNone)从任何地方调整大小,否则底部/右侧会遇到问题(微软的错,我确信谷歌搜索会验证这一点)。

如果您没有使用默认的 Windows ResizeMethod,您可以发布您的代码吗?否则,我必须假设问题出在您的 Windows 属性或包含整个窗口的基本元素之一中。

于 2012-12-16T10:23:33.967 回答