展开控件时调整窗口大小
本文关键字:调整 窗口大小 控件 | 更新日期: 2023-09-27 17:50:35
由我原来问题的解推导而来
我现在有以下xaml:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="300" Width="450" SizeToContent="WidthAndHeight">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="auto" />
</Grid.ColumnDefinitions>
<TabControl Grid.Column="0" MinWidth="200" Grid.Row="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
<Expander Grid.Column="1" Grid.Row="0" ExpandDirection="Right">
<StackPanel x:Name="cont">
<Label>testtesttesttesttest</Label>
<Label>testtesttesttesttest</Label>
<Label>testtesttesttesttest</Label>
<Label>testtesttesttesttest</Label>
<Label>testtesttesttesttest</Label>
<Label>testtesttesttesttest</Label>
<Label>testtesttesttesttest</Label>
<Label>testtesttesttesttest</Label>
</StackPanel>
</Expander>
</Grid>
</Window>
我想扩展窗口宽度每次我展开扩展器控件向右。当我运行上面引用的xaml并在构建后展开扩展器控件时,一切都像预期的那样工作,但是只要我手动调整窗口的大小,扩展器控件的内容就会展开到左边的现有区域。
我如何改变这种行为,使窗口将其宽度扩展到右侧,内容将结束在新的区域?
这不是WPF内置的东西,所以您必须编写一些自定义代码来处理它。SizeToContent的作用直到最终用户调整窗口大小,然后窗口大小是固定的。
你可以用这样的语句来完成你想要的:
public partial class MainWindow {
public MainWindow() {
InitializeComponent();
}
protected override void OnSourceInitialized(EventArgs e) {
base.OnSourceInitialized(e);
IntPtr handle = new WindowInteropHelper(this).Handle;
HwndSource.FromHwnd(handle).AddHook(new HwndSourceHook(this.WindowProc));
}
private const int WM_SIZING = 0x0214;
private const int WM_EXITSIZEMOVE = 0x0232;
private IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) {
switch (msg) {
case WM_SIZING:
this.firstColumn.ClearValue(ColumnDefinition.MinWidthProperty);
break;
case WM_EXITSIZEMOVE:
this.firstColumn.MinWidth = this.firstColumn.ActualWidth;
this.SizeToContent = System.Windows.SizeToContent.WidthAndHeight;
break;
}
return IntPtr.Zero;
}
}
然后你需要给你的第一个ColumnDefinition的名称"firstColumn",像这样:
<ColumnDefinition x:Name="firstColumn" Width="*" />
有效地使用了SizeToContent。如果你调整窗口的大小,那么它会确保第一列的最小大小保持固定的大小,并重新打开SizeToContent。
编辑:注意到你使用了VB。. NET标签,所以这里是VB。净版:
Public Partial Class MainWindow
Public Sub New()
InitializeComponent()
End Sub
Protected Overrides Sub OnSourceInitialized(e As EventArgs)
MyBase.OnSourceInitialized(e)
Dim handle As IntPtr = New WindowInteropHelper(Me).Handle
HwndSource.FromHwnd(handle).AddHook(New HwndSourceHook(AddressOf Me.WindowProc))
End Sub
Private Const WM_SIZING As Integer = &H214
Private Const WM_EXITSIZEMOVE As Integer = &H232
Private Function WindowProc(hwnd As IntPtr, msg As Integer, wParam As IntPtr, lParam As IntPtr, ByRef handled As Boolean) As IntPtr
Select Case msg
Case WM_SIZING
Me.firstColumn.ClearValue(ColumnDefinition.MinWidthProperty)
Exit Select
Case WM_EXITSIZEMOVE
Me.firstColumn.MinWidth = Me.firstColumn.ActualWidth
Me.SizeToContent = System.Windows.SizeToContent.WidthAndHeight
Exit Select
End Select
Return IntPtr.Zero
End Function
End Class