C#WPF滚动条更改/移动内容

时间:2016-04-29 08:59:52

标签: c# wpf xaml

我很难让我的WPF表单可滚动。表单很长,我希望用户能够向下滚动。如果我调整表单的高度,当滚动条变为活动状态时,它会移动所有内容,并使我的内容变小。我该如何避免这种情况?我添加了2张图片(之前和之后)和我的XAML代码。

enter image description here

enter image description here

<Window x:Class="RegisterForm.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="1000" Width="700" MinHeight="1000" MinWidth="700">

<ScrollViewer
    VerticalScrollBarVisibility="Visible"
    CanContentScroll="True">





<Grid>


    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
        </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>





        <Label
        Content="Full Name"
        FontSize="14"
        FontWeight="Bold"
        Grid.Row="1"
        Grid.ColumnSpan="3"
        Margin="40,0,40,0"
        HorizontalContentAlignment="Left">
    </Label>

    <Label
        Content="Gender"
        FontSize="14"
        FontWeight="Bold"
        Grid.Row="2"
        Grid.ColumnSpan="3"
        Margin="40,0,40,0"
        HorizontalContentAlignment="Left">
    </Label>

        <Label
        Content="Date of Birth"
        FontSize="14"
        FontWeight="Bold"
        Grid.Row="3"
        Grid.ColumnSpan="3"
        Margin="40,0,40,0"
        HorizontalContentAlignment="Left">
        </Label>

        <Label
        Content="Address"
        FontSize="14"
        FontWeight="Bold"
        Grid.Row="4"
        Grid.ColumnSpan="3"
        Margin="40,0,40,0"
        HorizontalContentAlignment="Left">
    </Label>

    <Label
        Content="Phone Number"
        FontSize="14"
        FontWeight="Bold"
        Grid.Row="7"
        Grid.ColumnSpan="3"
        Margin="40,0,40,0"
        HorizontalContentAlignment="Left">
    </Label>

    <Label
        Content="Phone Number Family"
        FontSize="14"
        FontWeight="Bold"
        Grid.Row="8"
        Grid.ColumnSpan="3"
        Margin="40,0,0,0"
        HorizontalContentAlignment="Left">
    </Label>

    <Label
        Content="E-mail"
        FontSize="14"
        FontWeight="Bold"
        Grid.Row="9"
        Grid.ColumnSpan="3"
        Margin="40,0,40,0"
        HorizontalContentAlignment="Left">
    </Label>

    <Label
        Content="Do you speak english?"
        FontSize="14"
        FontWeight="Bold"
        Grid.Row="10"
        Grid.ColumnSpan="3"
        Margin="40,0,0,0"
        HorizontalContentAlignment="Left">
    </Label>

    <Label
        Content="Education"
        FontSize="14"
        FontWeight="Bold"
        Grid.Row="11"
        Grid.ColumnSpan="3"
        Margin="40,0,40,0"
        HorizontalContentAlignment="Left">
    </Label>

    <Label
        Content="Job Type/Profession"
        FontSize="14"
        FontWeight="Bold"
        Grid.Row="12"
        Grid.ColumnSpan="3"
        Margin="40,0,0,0"
        HorizontalContentAlignment="Left">
    </Label>

    <Label
        Content="Experience"
        FontSize="14"
        FontWeight="Bold"
        Grid.Row="13"
        Grid.ColumnSpan="3"
        Margin="40,0,40,0"
        HorizontalContentAlignment="Left">
    </Label>

    <Label
        Content="Drivers License"
        FontSize="14"
        FontWeight="Bold"
        Grid.Row="14"
        Grid.ColumnSpan="3"
        Margin="40,0,40,0"
        HorizontalContentAlignment="Left">
    </Label>

    <TextBox
        x:Name="txtFirstName"
        FontSize="14"
        Grid.Row="1"
        Grid.Column="3"
        Grid.ColumnSpan="2"
        Margin="10,0,0,30"
        HorizontalContentAlignment="Center"
        VerticalContentAlignment="Center">
    </TextBox>

        <Label
        Content="First Name"
        FontSize="12"
        Grid.Row="1"
        Grid.Column="3"
        Grid.ColumnSpan="2"
        Margin="10,25,0,0"
        FontStyle="Italic"
        HorizontalContentAlignment="Left"
        HorizontalAlignment="Left">
        </Label>

        <TextBox
        x:Name="txtLastName"
        FontSize="14"
        Grid.Row="1"
        Grid.Column="5"
        Grid.ColumnSpan="3"
        Margin="10,0,0,30"
        HorizontalContentAlignment="Center"
        VerticalContentAlignment="Center">
        </TextBox>

        <Label
        Content="Last Name"
        FontSize="12"
        Grid.Row="1"
        Grid.Column="5"
        Grid.ColumnSpan="2"
        Margin="10,25,0,0"
        FontStyle="Italic"
        HorizontalContentAlignment="Left"
        HorizontalAlignment="Left">
        </Label>

        <ComboBox
            x:Name="CBGender"
            FontSize="12"
            Grid.Row="2"
            Grid.Column="3"
            Grid.ColumnSpan="2"
            Margin="10,0,0,30"
            IsEditable="True"
            IsReadOnly="True"
            Text="Please Select"
            FontStyle="Normal">
            <ListBoxItem Content="Male" FontStyle="Normal"/>
            <ListBoxItem Content="Female" FontStyle="Normal"/>
        </ComboBox>

        <ComboBox
            x:Name="CBDOBDay"
            FontSize="12"
            Grid.Row="3"
            Grid.Column="3"
            Grid.ColumnSpan="1"
            Margin="10,0,0,30"
            IsEditable="True"
            IsReadOnly="True"
            Text="DD"
            FontStyle="Normal">
        </ComboBox>

        <ComboBox
            x:Name="CBDOBMonth"
            FontSize="12"
            Grid.Row="3"
            Grid.Column="4"
            Grid.ColumnSpan="1"
            Margin="10,0,0,30"
            IsEditable="True"
            IsReadOnly="True"
            Text="MM"
            FontStyle="Normal">
        </ComboBox>

        <ComboBox
            x:Name="CBDOBYear"
            FontSize="12"
            Grid.Row="3"
            Grid.Column="5"
            Grid.ColumnSpan="1"
            Margin="10,0,0,30"
            IsEditable="True"
            IsReadOnly="True"
            Text="YY"
            FontStyle="Normal">
        </ComboBox>

        <TextBox
        x:Name="txtStreetAddress"
        FontSize="14"
        Grid.Row="4"
        Grid.Column="3"
        Grid.ColumnSpan="3"
        Margin="10,0,0,30"
        HorizontalContentAlignment="Center"
        VerticalContentAlignment="Center">
        </TextBox>

        <Label
        Content="Street Address"
        FontSize="12"
        Grid.Row="4"
        Grid.Column="3"
        Grid.ColumnSpan="2"
        Margin="10,25,0,0"
        FontStyle="Italic"
        HorizontalContentAlignment="Left"
        HorizontalAlignment="Left">
        </Label>

        <TextBox
        x:Name="txtCity"
        FontSize="14"
        Grid.Row="5"
        Grid.Column="3"
        Grid.ColumnSpan="2"
        Margin="10,0,0,30"
        HorizontalContentAlignment="Center"
        VerticalContentAlignment="Center">
        </TextBox>

        <Label
        Content="City"
        FontSize="12"
        Grid.Row="5"
        Grid.Column="3"
        Grid.ColumnSpan="2"
        Margin="10,25,0,0"
        FontStyle="Italic"
        HorizontalContentAlignment="Left"
        HorizontalAlignment="Left">
        </Label>

        <TextBox
        x:Name="txtStateProvince"
        FontSize="14"
        Grid.Row="5"
        Grid.Column="5"
        Grid.ColumnSpan="3"
        Margin="10,0,0,30"
        HorizontalContentAlignment="Center"
        VerticalContentAlignment="Center">
        </TextBox>

        <Label
        Content="State / province"
        FontSize="12"
        Grid.Row="5"
        Grid.Column="5"
        Grid.ColumnSpan="2"
        Margin="10,25,0,0"
        FontStyle="Italic"
        HorizontalContentAlignment="Left"
        HorizontalAlignment="Left">
        </Label>

        <TextBox
        x:Name="txtPostalZipCode"
        FontSize="14"
        Grid.Row="6"
        Grid.Column="3"
        Grid.ColumnSpan="2"
        Margin="10,0,0,30"
        HorizontalContentAlignment="Center"
        VerticalContentAlignment="Center">
        </TextBox>

        <Label
        Content="Postal / Zip code"
        FontSize="12"
        Grid.Row="6"
        Grid.Column="3"
        Grid.ColumnSpan="2"
        Margin="10,25,0,0"
        FontStyle="Italic"
        HorizontalContentAlignment="Left"
        HorizontalAlignment="Left">
        </Label>

        <ComboBox
            x:Name="CBCountry"
            FontSize="12"
            Grid.Row="6"
            Grid.Column="5"
            Grid.ColumnSpan="3"
            Margin="10,0,0,30"
            IsEditable="True"
            IsReadOnly="True"
            Text="Please Select"
            FontStyle="Normal" SelectionChanged="CBCountry_SelectionChanged">
        </ComboBox>


        <Label
        Content="Country"
        FontSize="12"
        Grid.Row="6"
        Grid.Column="5"
        Grid.ColumnSpan="2"
        Margin="10,25,0,0"
        FontStyle="Italic"
        HorizontalContentAlignment="Left"
        HorizontalAlignment="Left">
        </Label>

        <TextBox
        x:Name="txtPhone1AreaCode"
        FontSize="14"
        Grid.Row="7"
        Grid.Column="3"
        Grid.ColumnSpan="2"
        Margin="10,0,0,30"
        HorizontalContentAlignment="Center"
        VerticalContentAlignment="Center">
        </TextBox>

        <TextBox
        x:Name="txtPhone1"
        FontSize="14"
        Grid.Row="7"
        Grid.Column="5"
        Grid.ColumnSpan="3"
        Margin="10,0,0,30"
        HorizontalContentAlignment="Center"
        VerticalContentAlignment="Center">
        </TextBox>

        <Label
        Content="Area Code (ex. +45)"
        FontSize="12"
        Grid.Row="7"
        Grid.Column="3"
        Grid.ColumnSpan="2"
        Margin="10,25,0,0"
        FontStyle="Italic"
        HorizontalContentAlignment="Left"
        HorizontalAlignment="Left">
        </Label>

        <Label
        Content="Phone Number"
        FontSize="12"
        Grid.Row="7"
        Grid.Column="5"
        Grid.ColumnSpan="2"
        Margin="10,25,0,0"
        FontStyle="Italic"
        HorizontalContentAlignment="Left"
        HorizontalAlignment="Left">
        </Label>

        <TextBox
        x:Name="txtPhone2AreaCode"
        FontSize="14"
        Grid.Row="8"
        Grid.Column="3"
        Grid.ColumnSpan="2"
        Margin="10,0,0,30"
        HorizontalContentAlignment="Center"
        VerticalContentAlignment="Center">
        </TextBox>

        <TextBox
        x:Name="txtPhone2"
        FontSize="14"
        Grid.Row="8"
        Grid.Column="5"
        Grid.ColumnSpan="3"
        Margin="10,0,0,30"
        HorizontalContentAlignment="Center"
        VerticalContentAlignment="Center">
        </TextBox>

        <Label
        Content="Area Code (ex. +45)"
        FontSize="12"
        Grid.Row="8"
        Grid.Column="3"
        Grid.ColumnSpan="2"
        Margin="10,25,0,0"
        FontStyle="Italic"
        HorizontalContentAlignment="Left"
        HorizontalAlignment="Left">
        </Label>

        <Label
        Content="Phone Number"
        FontSize="12"
        Grid.Row="8"
        Grid.Column="5"
        Grid.ColumnSpan="2"
        Margin="10,25,0,0"
        FontStyle="Italic"
        HorizontalContentAlignment="Left"
        HorizontalAlignment="Left">
        </Label>

        <TextBox
        x:Name="txtEmail"
        FontSize="14"
        Grid.Row="9"
        Grid.Column="3"
        Grid.ColumnSpan="4"
        Margin="10,0,0,30"
        HorizontalContentAlignment="Center"
        VerticalContentAlignment="Center">
        </TextBox>

        <Label
        Content="Ex. myname@example.com"
        FontSize="12"
        Grid.Row="9"
        Grid.Column="3"
        Grid.ColumnSpan="3"
        Margin="10,25,0,0"
        FontStyle="Italic"
        HorizontalContentAlignment="Left"
        HorizontalAlignment="Left">
        </Label>





    </Grid>

</ScrollViewer>

//谢谢,Rasmus

2 个答案:

答案 0 :(得分:1)

您必须指定至少一个部分的Heigh或MinHeigh:编辑器(标签/文本框/组合框)或GridRows或整个网格。当然,网格填充在窗口中,列也将整个网格填充在一起。通常你会将网格的行高设置为

<RowDefinition Height="auto" />

而不是

<RowDefinition Height="*" />

并为编辑提供某种MinHeight或Padding。

所以有很多解决方案。这取决于您希望如何获得标签/编辑器高度。 (动态或固定)

修改/ ADD: 就像SideNote一样,如果你定义那些Row- /和ColumnDefinitions的相同样式,你可能应该在Grid中为它们分配一个Style。 它使得一次更改变得更容易; D

<Grid>
    <Grid.Resources>
        <Style TargetType="RowDefinition">
            <Setter Property="Height" Value="auto" />
        </Style>
    </Grid.Resources>

    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>

    <!-- ... -->


</Grid>

答案 1 :(得分:1)

ScrollViewer的问题在于它实际上是无限的。因此,其中的内容无法计算相对大小,例如*。在你的情况下,我认为如果你为你的网格添加一个高度,像<Grid Height="1000">这样的东西就可以了。