源自我原始问题的解决方案here
我现在有以下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并在构建之后展开扩展器控件时,一切都按预期工作,但是一旦我手动调整窗口大小,扩展器控件的内容将扩展到左边的现有区域。
如何更改此行为,以便窗口将其宽度扩展到右侧,内容将最终在该新区域中?
答案 0 :(得分:2)
这不是内置于WPF中的内容,因此您必须编写一些自定义代码来处理它。 SizeToContent一直有效,直到最终用户调整窗口大小,然后修复Window大小。
你可以使用这样的东西来完成你想要的东西:
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.NET版本:
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