wpf - 如何在窗口调整大小时调整文本框的大小

标签 wpf

设置任何属性以使文本框根据窗口大小调整大小?

最佳答案

WPF 中的布局受父容器的影响很大。例如,如果您要创建带有标签和输入字段的表单,请考虑使用“网格”面板。默认情况下,WPF 中的控件根据其父级的布局行为调整大小。下面是一个带有两个标签文本框和两个随窗口一起调整大小的按钮的窗口示例。

<Window>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Label Content="Contact Name" Grid.Row="0" Grid.Column="0" />
        <TextBox Grid.Row="0" Grid.Column="1" />

        <Label Content="Contact Location" Grid.Row="1" Grid.Column="0" />
        <TextBox Grid.Row="1" Grid.Column="1" />

        <StackPanel Orientation="Horizontal" HorizontalAlignment="Right"
                    VerticalAlignment="Bottom" Grid.Row="2" Grid.Column="1">
            <Button Content="OK" Width="75" Height="24" Margin="3" />
            <Button Content="Cancel" Width="75" Height="24" Margin="3" />
        </StackPanel>

    </Grid>
</Window>

或者,如果您想要类似于浏览器地址栏布局的内容,您可以执行以下操作:
<Window>
    <DockPanel>
        <DockPanel DockPanel.Dock="Top">
            <Button Content="Back" DockPanel.Dock="Left" />
            <Button Content="Forward" DockPanel.Dock="Left" />
            <Button Content="Refresh" DockPanel.Dock="Right" />
            <TextBox /> <!-- fill is assumed for last child -->
        <DockPanel>
        <StatusBar DockPanel.Dock="Bottom" />
        <WebBrowser /> <!-- fill is assumed for last child -->
    </DockPanel>
</Window>

请注意,在上面的示例中,我嵌套了两个 DockPanel。它也可以通过 Grid 实现,但标记会更加困惑。如果您不熟悉 WPF,我强烈建议您使用各种可用的面板。一旦您了解何时将特定面板应用于特定布局,它就会使 WPF 更易于使用。

关于wpf - 如何在窗口调整大小时调整文本框的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4495947/

相关文章:

wpf - 尝试在 WPF 中设置子 DataGrid 的 itemsource 时出现 "Items collection must be empty before using ItemsSource."之类的异常

wpf - 不允许在 VS2010 扩展中的 WPF 工具窗口中拖放

wpf - PRISM RegionManager - TabControl 选项卡顺序

c# - 在 wpf 中重新加载图像

WPF 从 x :static for doubles 定义一个静态资源

wpf - 为什么在我使用 ParserContext 时 XamlReader 会抛出异常?

c# - Window.Loaded - ShowDialog() - 无法执行操作

.net - 来自作为字符串的友好名称的 WPF 颜色

wpf - 为什么这个特定的图像会导致我的应用程序因 OutOfMemoryException 而崩溃?

c# - DataGrid ComboBox 第一次点击时没有选中项