当我用鼠标拖动 window 时如何调整整个 window 的大小

How resize whole window when I drag window with mouse

我是 WPF 控件的新手,在我使用 WinForms 应用程序之前,如果我将锚点放在控件上并将停靠点放在容器上,一切都会顺利进行,但在这里我遇到了错误的地方?

<Window x:Class="ChatApp.Client.ClientWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ChatApp.Client"
        mc:Ignorable="d"
        Title="Client" Height="450" Width="800">
    <Grid>
        <DockPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
            <StackPanel DockPanel.Dock="Top" Orientation="Horizontal" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
                <GroupBox Header="Client" Height="Auto">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Margin="5, 0, 5, 0">Address:</TextBlock>
                        <TextBox x:Name="txtAddress" Width="80"></TextBox>
                        <TextBlock Margin="10, 0, 5, 0">Port:</TextBlock>
                        <TextBox x:Name="txtPort" Width="80"></TextBox>
                        <Button x:Name="btnConnect" Margin="430, 0, 5, 0" Content="Connect" Width="80" Click="btnConnect_Click"/>
                    </StackPanel>
                </GroupBox>
            </StackPanel>
            <StackPanel DockPanel.Dock="Left" Orientation="Horizontal" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
                <GroupBox Header="Chat" Width="650" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
                    <TextBox x:Name="txtConversation" AcceptsReturn="True"></TextBox>
                </GroupBox>
            </StackPanel>
            <StackPanel DockPanel.Dock="Right" Orientation="Horizontal" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
                <GroupBox Header="Users" Width="135" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
                    <ListBox x:Name="lbUsers" Height="Auto" Margin="5,0" VerticalAlignment="Stretch">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <StackPanel>
                                    <TextBlock Text="{Binding}"/>
                                </StackPanel>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                </GroupBox>
            </StackPanel>
        </DockPanel>
    </Grid>
</Window>

我有三个组框顶部停靠栏,第二个左侧停靠栏,最后一个用于右侧停靠栏。 当我想用鼠标右键调整表单大小时分组框不想拉伸时 我调整表格大小。

您的布局似乎过于复杂 - 您只是想在 WPF 中复制您在 Winforms 中所做的事情吗?

  • 让每个只有一个子控件的 StackPanel 或 Grid 没有任何意义。
  • 尝试用适当大小的网格行和列替换您的 DockPanel。
  • 如果您硬编码它们的宽度,GroupBox 将不会调整大小。
  • 无需为 ListBox 定义 ItemTemplate 即可显示单个字符串。

像下面这样的东西将是您布局的一个很好的起点。

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
  
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="4*"  />
        <ColumnDefinition Width="*" MinWidth="135" />
    </Grid.ColumnDefinitions>

    <GroupBox Header="Client" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Margin="5">
        <Grid >
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
          
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
    
            <TextBlock Grid.Row="0" Grid.Column="0" Text="Address:"/>
            <TextBox x:Name="txtAddress" Width="80" Grid.Row="1" Grid.Column="0" />
            <TextBlock Grid.Row="2" Grid.Column="0" Text="Port:" />
            <TextBox x:Name="txtPort" Width="80" Grid.Row="3" Grid.Column="0" />
            <Button x:Name="btnConnect" Grid.Row="0" Grid.Column="2" Content="Connect" Width="80" />
        </Grid>
    </GroupBox>
  
    <GroupBox Header="Chat" Grid.Row="1" Grid.Column="0" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
        <TextBox x:Name="txtConversation" AcceptsReturn="True" />
    </GroupBox>
  
    <GroupBox Header="Users" Grid.Row="1" Grid.Column="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
        <ListBox x:Name="lbUsers" />
    </GroupBox>
</Grid>