WPF窗口拉伸不正确/奇怪的行为

时间:2012-12-16 08:23:48

标签: wpf layout tabcontrol stackpanel dockpanel

我现在在WPF中有一段奇怪的行为,而且我无法追查问题所在。简而言之,当我从底部或右侧调整窗口大小时,一切都按预期工作。但是,如果我碰巧从顶部或左侧抓住它,它会延伸窗口而不是窗口内容。我试过围绕wirh HorizontalContentAlignmentHorizontalAlignmentVerticalContentAlignment和& VerticalAlignment无济于事。任何人都有任何想法存在的问题?

从左侧调整大小:

从顶部调整大小:

从右下角调整大小:

这是我正在使用的XAML,为简洁起见删除了内部控件:

窗口XAML设置:

<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">

容器XAML设置:

<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}">

更新:按要求更改LocationChanged代码

    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;
        }
    }

原来这就是问题,因为当我删除它的XAML部分时问题得到纠正。我想要保留这种方法,因为有一种方法依赖它来保持ChildWindows MainWindow被锁定到边缘。如果可能,我想继续使用它。

1 个答案:

答案 0 :(得分:0)

您使用的是自定义Window Resize方法吗?我根据自己的经验知道,除非你使用默认的Windows ResizeMethod(如在WindowStyle = AnythingButNone中)从任何地方调整大小,否则Bottom / Right会遇到问题(微软的错,我肯定会有一点谷歌搜索验证那)。

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