如何禁用窗口自动最大化?

时间:2011-10-04 17:55:06

标签: c# wpf xaml window-resize

我有一个窗口,我已设置ResizeMode = NoResize并摆脱所有标题栏和按钮,但问题是当窗口被拖动到屏幕顶部时它最大化,我无法阻止它。在我有代码最大化并将窗口最小化到某个宽度和高度之前,是否有人遇到过这个问题。

这是一个示例代码

<Window xmlns:my="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit"
    x:Class="Custom_title_bar.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" WindowStyle="None" AllowsTransparency="True" 
    Background="Transparent" mc:Ignorable="d" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    ResizeMode="NoResize"  
    MinHeight="180" MinWidth="180"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    SizeToContent="WidthAndHeight" WindowStartupLocation="CenterScreen" 
    VerticalContentAlignment="Center" VerticalAlignment="Top" WindowState="Normal">
<Grid Name="Main" >
   <Border Name="MainWindow" CornerRadius="1" Background="White"   
    BorderBrush="AliceBlue" MouseDown="move_window"  >
      <Grid>
        <DockPanel>
           <DockPanel DockPanel.Dock="Top" Height="26">
              <Border CornerRadius="1">
                 <Border.Background>
                     <LinearGradientBrush>
                        <GradientStop Color="White" Offset="0.0"/>
                        <GradientStop Color="BurlyWood" Offset="0.25"/>
                        <GradientStop Color="Bisque" Offset="0.5"/>
                      </LinearGradientBrush>
                  </Border.Background>
                 <Grid>
                   <DockPanel>  
                    <Image  MouseDown="MINIMIZE" 
                     Source="/Custom%20title%20bar;component/Images/minimize.png" 
                     Grid.ColumnSpan="4" />
                     <Image  MouseDown="MAX_RESTORE" 
                      Source="/Custom%20title%20bar;component/Images/Restore.png"                   
                      Grid.ColumnSpan="4" />
                      <Image  MouseDown="EXIT" 
                      Source="/Custom%20title%20bar;component/Images/close.png"  />
                       <TextBlock/>
                    </DockPanel>
                 </Grid>
            </Border>
         </DockPanel>
       </DockPanel>
      </Grid>
     </Border>
  </Grid>

1 个答案:

答案 0 :(得分:3)

为什么不设置MaxHeight / MaxWidth属性?

由于您的默认尺寸似乎为SizeToContent="WidthAndHeight",因此最好在MaxHeight的{​​{1}}事件中设置MaxWidth / Loaded < / p>

Window