c# - 最大化/最小化时,如何调整 TextBox 的大小以适应窗口大小?

标签 c# wpf

当我最大化/最小化它时,如何调整我的 TextBoxe/Label 的大小以适应窗口大小?此时文本框刚好位于窗口的中心。我希望他们调整大小以适应网格列和行的大小。我怎样才能做到这一点?我试图确定文本框属于什么列和行,虽然它会根据 ColumnDefinition 相应地调整大小,但这并没有改变任何东西。

<DockPanel Margin="2,46,0,0" Name="dockPanel1" Width="Auto">
        <TabControl Height="Auto" Name="tabControl1" Width="Auto" VerticalAlignment="Stretch" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" DockPanel.Dock="Top">
            <TabItem Header="tabItem1" Name="tabItem1">
                <Grid Name="grid1" HorizontalAlignment="Center" VerticalAlignment="Top" Height="523" Width="766">
                    <Grid.RowDefinitions>
                        <RowDefinition MinHeight="0" Height="43*" />
                        <RowDefinition Height="45*" />
                        <RowDefinition Height="435*" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition MinWidth="0" Width="Auto" />
                        <ColumnDefinition Width="Auto" MinWidth="100" />
                        <ColumnDefinition Width="385*" />
                    </Grid.ColumnDefinitions>

                    <TextBox Grid.Column="0" Grid.Row="0" Height="27" Name="textBox1" VerticalAlignment="Top" Margin="0,4,0,0" HorizontalAlignment="Right" Width="141" FontSize="14" HorizontalContentAlignment="Stretch" />
                    <Label Content="Label" Height="9" HorizontalAlignment="Left" Margin="16,7,0,0" Name="label1" VerticalAlignment="Top" Width="35" />
                    <Label Content="Supplier" Height="27" Name="label2" VerticalAlignment="Top" FontSize="14" FontFamily="Tahoma" FontWeight="Bold" Margin="6,4,0,0" Width="133" IsEnabled="True" HorizontalAlignment="Left" Foreground="Black" Background="White" />
                    <TextBox FontSize="14" Height="27" HorizontalAlignment="Stretch" Margin="140,6,0,0" Name="textBox11" VerticalAlignment="Top" Width="141" Grid.Row="1" />
                    <Label Content="Supplier Bank" FontFamily="Tahoma" FontSize="14" FontWeight="Bold" Height="27" Margin="6,6,142,0" Name="label3" VerticalAlignment="Top" Width="133" Grid.Row="1" Background="White" />
                    <TextBox FontSize="14" Height="27" HorizontalAlignment="Stretch" Margin="140,7,0,0" Name="textBox12" VerticalAlignment="Top" Width="141" Grid.Row="2" />
                    <Label Content="Account Number" FontFamily="Tahoma" FontSize="14" FontWeight="Bold" Height="27" Margin="6,7,142,0" Name="label4" VerticalAlignment="Top" Width="133" Grid.Row="2" Background="White" />
                    <TextBox FontSize="14" Height="27" Margin="134,4,110,0" Name="textBox2" VerticalAlignment="Top" Grid.Column="2" HorizontalAlignment="Stretch" Width="141" DataContext="{Binding ElementName=grid1, Path=ActualWidth}" />
                    <Label Content="Buyer" FontFamily="Tahoma" FontSize="14" FontWeight="Bold" Height="27" Margin="0,4,252,0" Name="label5" VerticalAlignment="Top" Width="133" Grid.Column="2" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Background="White" />
                    <TextBox FontSize="14" Grid.Row="1" Height="27" HorizontalAlignment="Left" Margin="134,6,0,0" Name="textBox3" VerticalAlignment="Top" Width="141" Grid.Column="2" />
                    <Label Content="Buyer Bank" FontFamily="Tahoma" FontSize="14" FontWeight="Bold" Grid.Row="1" Height="27" Margin="0,6,0,0" Name="label6" VerticalAlignment="Top" Width="133" Grid.Column="2" HorizontalAlignment="Left" Background="White" />
                    <TextBox FontSize="14" Grid.Row="2" Height="27" HorizontalAlignment="Left" Margin="134,7,0,0" Name="textBox4" VerticalAlignment="Top" Width="141" Grid.Column="2" />
                    <Label Content="Account Number" FontFamily="Tahoma" FontSize="14" FontWeight="Bold" Grid.Row="2" Height="27" Margin="0,7,0,0" Name="label7" VerticalAlignment="Top" Width="133" Grid.Column="2" HorizontalAlignment="Left" Background="White" />
                </Grid>
            </TabItem>
            <TabControl.Background>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="Black" Offset="0.035" />
                    <GradientStop Color="#FF1B327A" Offset="0.528" />
                    <GradientStop Color="#FF001E98" Offset="0.278" />
                </LinearGradientBrush>
            </TabControl.Background>
        </TabControl>
    </DockPanel>

最佳答案

不要为宽度属性使用静态值。

您可以尝试“Horizo​​ntalAlignment=Stretch”。

关于c# - 最大化/最小化时,如何调整 TextBox 的大小以适应窗口大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31639521/

相关文章:

c# - 将平面 xml 反序列化为实体

c# - .NET 中广泛使用 DAO 模式吗?

c# - Like 不会转义特殊字符 NHibernate

c# - 亲子关系界面

c# - 如何在asp.net mvc 4中上传图片并显示在同一页面上

c# - Android 模拟器未运行 - Visual Studio 2017

wpf - 如何在代码隐藏(MahApps)中将水印属性分配给文本框的文本

wpf - .net 4 中的 MVVM 有什么新功能?

c# - WPF 和 MVVM : CloseAction don't work

wpf - 在 WPF 中,如何使用复制文本选项(或)创建带有文本修剪选项的文本框