gpt4 book ai didi

c# - 扩展控件时调整窗口大小

转载 作者:太空宇宙 更新时间:2023-11-03 11:37:45 27 4
gpt4 key购买 nike

源 self 原来问题的解决方案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 并在构建后展开扩展器控件时,一切都按预期工作,但只要我手动调整窗口大小,扩展器控件的内容就会扩展到左侧的现有区域。

我怎样才能改变这种行为,以便窗口将其宽度扩展到右侧,并且内容将最终出现在那个新区域中?

最佳答案

这不是 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.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

关于c# - 扩展控件时调整窗口大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5709053/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com