WPF 格式问题 - 自动拉伸和调整大小? [英] WPF Formatting Issues - Automatically stretching and resizing?

查看:27
本文介绍了WPF 格式问题 - 自动拉伸和调整大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 WPF 和 XAML 非常陌生.我正在尝试设计一个基本的数据输入表单.我使用了一个包含四个堆栈面板的堆栈面板来获得我想要的布局.也许网格会更好,我不确定.

I'm very new to WPF and XAML. I am trying to design a basic data entry form. I have used a stack panel holding four more stack panels to get the layout I want. Perhaps a grid would be better for this, I am not sure.

这是生成它的 XAML 代码:

And here is the XAML code that generates it:

<Window x:Class="Test1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="224" Width="536.762">
    <StackPanel Height="Auto" Name="stackPanel1" Width="Auto" Orientation="Horizontal">
        <StackPanel Height="Auto" Name="stackPanel2" Width="Auto">
            <Label Height="Auto" Name="label1" Width="Auto">Patient Name:</Label>
            <Label Height="Auto" Name="label2" Width="Auto">Physician:</Label>
            <Label Height="Auto" Name="label3" Width="Auto">Insurance:</Label>
            <Label Height="Auto" Name="label4" Width="Auto">Therapy Goals:</Label>
        </StackPanel>
        <StackPanel Height="Auto" Name="stackPanel3" Width="Auto">
            <TextBox Height="Auto" Name="textBox1" Width="Auto" Padding="3" Margin="1" />
            <TextBox Height="Auto" Name="textBox2" Width="Auto" Padding="3" Margin="1" />
            <TextBox Height="Auto" Name="textBox3" Width="Auto" Padding="3" Margin="1" />
            <TextBox Height="Auto" Name="textBox4" Width="Auto" Padding="3" Margin="1" />
        </StackPanel>
        <StackPanel Height="Auto" Name="stackPanel4" Width="Auto">
            <Label Height="Auto" Name="label5" Width="Auto">Date:</Label>
            <Label Height="Auto" Name="label6" Width="Auto">Patient Phone:</Label>
            <Label Height="Auto" Name="label7" Width="Auto">Facility:</Label>
            <Label Height="Auto" Name="label8" Width="Auto">Referring Physician:</Label>
        </StackPanel>
        <StackPanel Height="Auto" Name="stackPanel5" Width="Auto">
            <TextBox Height="Auto" Name="textBox5" Width="Auto" Padding="3" Margin="1" />
            <TextBox Height="Auto" Name="textBox6" Width="Auto" Padding="3" Margin="1" />
            <TextBox Height="Auto" Name="textBox7" Width="Auto" Padding="3" Margin="1" />
            <TextBox Height="Auto" Name="textBox8" Width="Auto" Padding="3" Margin="1" />
        </StackPanel>
    </StackPanel>
</Window>

我真正想要的是让文本框均匀拉伸以水平填充空间.我还希望每个垂直堆栈面板中的控件在垂直调整窗口大小时均匀展开".

What I really want is for the text boxes to stretch equally to fill up the space horizontally. I would also like for the controls in each vertical stackpanel to 'spread out' evenly as the window is resized vertically.

推荐答案

StackPanel 始终根据其方向将其子项与顶部或左侧边缘对齐.听起来你想要的是一个 UniformGrid ,你的外部 StackPanel 所在的位置.试试这个:

StackPanel always aligns its children against the top or left edge depending upon its orientation. It sounds like what you want is a UniformGrid where your outer StackPanel is. Try this:

<Window>
    <UniformGrid Name="stackPanel1" Rows="1">
        <StackPanel Name="stackPanel2">
           ...
        </StackPanel>
        <StackPanel Name="stackPanel3">
           ...
        </StackPanel>
        <StackPanel Name="stackPanel4">
           ...
        </StackPanel>
        <StackPanel Name="stackPanel5">
           ...
        </StackPanel>
    </UniformGrid>
</Window>

请注意,您不需要设置 Width=Auto 或 Height=Auto,这是隐含的.

Note that you don't need to set Width=Auto or Height=Auto, this is implied.

但是您说 Grid 可能更好(即使 XAML 很难看)是对的,因为在这种布局配置中,您的标签很容易与文本框不对齐.你也可以试试这样的...

But you are right that a Grid is probably better (even though the XAML is ugly) because in this layout configuration your labels could easily get out of alignment with the text boxes. You can try something like this too...

<UniformGrid Rows="1">

    <Grid>

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

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


        <Label Grid.Row="1" Grid.Column="0" Content="Field 2" />
        <TextBox Grid.Row="1" Grid.Column="1" />


        <Label Grid.Row="2" Grid.Column="0" Content="Field 3" />
        <TextBox Grid.Row="2" Grid.Column="1" />

    </Grid>

    <Grid /> <!-- repeat above -->

    <Grid /> <!-- etc... -->

</UniformGrid>

这篇关于WPF 格式问题 - 自动拉伸和调整大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆