我只想让我的WPF窗口水平调整大小。 我如何才能做到这一点?WPF窗口 - 只允许水平调整大小
回答
如果设置窗口的MinHeight
和MaxHeight
属性到理想高度的窗口都会有一个固定的高度
当你想到它的时候,这样做很有效。谢谢 – 2010-06-24 13:53:52
不幸的是,与基于内容大小的自动高度计算不兼容。 – 2012-03-28 22:06:36
这是一种痛苦。基本上你需要设置一个钩子函数来处理Windows消息。然后,您将捕获WM_SIZING(0x0214)消息并修改参数,以便不能更改水平尺寸。
Pete Brown在他的博客上也有关于此主题的一些很好的信息。
您可以尝试数据绑定到窗口的大小,然后在更改垂直尺寸时将大小设置回旧值。
唯一的缺点是它是邮政处理。 – Amsakanna 2010-06-23 09:43:31
确实如此。但在大多数情况下,我怀疑用户不会注意到速度会很快。 – 2010-06-23 10:55:54
如果你想使用MinHeight
和MaxHeight
的做法,但仍允许窗口自动调整大小本身适合大小其内容:
<Window x:Class="MyWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
SizeToContent="WidthAndHeight"
ResizeMode="CanResize"
Loaded="window_Loaded">
在后台代码:
private void window_Loaded(object sender, RoutedEventArgs e)
{
this.MinWidth = this.ActualWidth;
this.MinHeight = this.ActualHeight;
this.MaxHeight = this.ActualHeight;
}
正如Tim Richards所建议的那样,在注意到“TextBox”和“Button”的下边框被剪裁在“Grid”中后,我必须将'MinHeight'和'MaxHeight'属性设置为'ContentRendered'事件在“GroupBox”中。 – 2017-06-26 07:36:03
如果要使用MinHeight和MaxHeight方法,但仍允许窗口自动调整大小以适合其内容的大小:
要允许自动调整内容大小,请不要使用Loaded事件。改用ContentRendered事件。
你能否粘贴这个报价的来源? – Tomasito 2015-01-28 23:57:11
如果您有以下要求: *宽可被用户调整(ResizeMode = CanResize) *高度自动调整大小(SizeToContent =身高)
它不会,原因有二: *有是否ResizeMode = CanResizeHeight *当用户调整窗口大小时,它会将SizeToContent重新设置为“手动”
我使用的一个简单黑客就是不断强制“SizeToContent”回到我想要的值。
<Window x:Class="MyWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
SizeToContent="Height"
ResizeMode="CanResize"
LayoutUpdated="LayoutUpdated">
private void LayoutUpdated(object sender, EventArgs e)
{
SizeToContent = SizeToContent.Height;
}
您还可以使用ContentRendered事件。 PropertyChanged事件不起作用。这并不完美,因为用户仍然可以垂直移动光标,如果快速完成,会导致闪烁。
以下解决方案可以让你离开SizeToContent
完好,
在窗口不需要Win32 Interop
或后面的代码:
它不依赖于Reactive Extensions
在Package Manager
运行:
Install-Package System.Reactive
然后你开始创建一个新的Behavior
:
public class VerticalResizeWindowBehavior : Behavior<UIElement>
{
public static readonly DependencyProperty MinHeightProperty = DependencyProperty.Register("MinHeight", typeof(double), typeof(VerticalResizeWindowBehavior), new PropertyMetadata(600.0));
public double MinHeight
{
get { return (double)GetValue(MinHeightProperty); }
set { SetValue(MinHeightProperty, value); }
}
protected override void OnAttached()
{
base.OnAttached();
var window = Window.GetWindow(AssociatedObject);
var mouseDown = Observable.FromEventPattern<MouseButtonEventArgs>(AssociatedObject, "MouseLeftButtonDown")
.Select(e => e.EventArgs.GetPosition(AssociatedObject));
var mouseUp = Observable.FromEventPattern<MouseButtonEventArgs>(AssociatedObject, "MouseLeftButtonUp")
.Select(e => e.EventArgs.GetPosition(AssociatedObject));
var mouseMove = Observable.FromEventPattern<MouseEventArgs>(AssociatedObject, "MouseMove")
.Select(e => e.EventArgs.GetPosition(AssociatedObject));
var q = from start in mouseDown
from position in mouseMove.TakeUntil(mouseUp)
select new { X = position.X - start.X, Y = position.Y - start.Y };
mouseDown.Subscribe(v => AssociatedObject.CaptureMouse());
mouseUp.Subscribe(v => AssociatedObject.ReleaseMouseCapture());
q.ObserveOnDispatcher().Subscribe(v =>
{
var newHeight = window.Height + v.Y;
window.Height = newHeight < MinHeight ? MinHeight : newHeight;
});
}
}
然后你在你的窗口底部添加一个UIElement
和应用Behavior
:
<Border Background="Gray"
Height="10"
Cursor="SizeNS"
Grid.ColumnSpan="2">
<i:Interaction.Behaviors>
<b:VerticalResizeWindowBehavior MinHeight="600"/>
</i:Interaction.Behaviors>
</Border>
设置你的窗口下面的属性:
ResizeMode="NoResize"
SizeToContent="Width"
注:在这个例子中,用户可以调整Vertically
而不是Horizontally
您可以轻松更改代码以允许opp osite,或添加属性
使其可配置。
哇,我认为将MinHeight,Height和MaxHeight设置为相同的值有效,但它没有。很高兴知道。 – DHN 2010-06-23 07:02:05