调整网格中的用户控件和对象的大小wpf

时间:2015-12-24 11:59:31

标签: c# wpf resize window

我有一个WPF应用程序,我希望当用户将窗口大小调整为更小的尺寸时,所有控件都保持相同的高度和宽度,但滚动条会出现。我首先将网格行和列,高度和宽度设置为自动,但是当我这样做时,我将所有控件都移出页面。当我在行和列上设置大小时,一切看起来都很完美,除了窗口缩小时,一切都被压扁了。解决这个问题的最佳解决方案是什么?我的xaml代码的一部分如下所示

XAML

    <Grid Background="White" x:Name="GeologicalAnalysisUI">
    <Grid.RowDefinitions>
        <RowDefinition Height="20"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="255"/>
        <ColumnDefinition Width="0.8*"/>
        <ColumnDefinition Width="1*"/>
        <ColumnDefinition Width="1.3*"/>
        <ColumnDefinition Width="1.3*"/>
    </Grid.ColumnDefinitions>
    <Border BorderBrush="DimGray" Grid.Row="1" Grid.Column="1" BorderThickness="1" Grid.ColumnSpan="3" Margin="0,2,0,2" Grid.RowSpan="6">
    </Border>
    <Border BorderBrush="DimGray" Grid.Row="1" Grid.Column="4" BorderThickness="1" Margin="5,2,0,2" Grid.RowSpan="6">
    </Border>
    <Border Grid.Column="1" Grid.Row="0" BorderBrush="DimGray" BorderThickness="1,1,1,0" Margin="0,0,0,0" Height="23" Width="180" HorizontalAlignment="Left" VerticalAlignment="Top" Background="White" Grid.RowSpan="2">
        <TextBlock HorizontalAlignment="Left" Height="16" Margin="10,3,0,0" TextWrapping="Wrap" VerticalAlignment="Center" Width="233" FontSize="11"><Run Language="en-gb" Text="Geological characterisation"/><LineBreak/><Run Language="en-gb"/></TextBlock>
    </Border>
    <Border Grid.Column="4" Grid.Row="0" BorderBrush="DimGray" BorderThickness="1,1,1,0" Margin="5,0,0,0" Height="23" Width="150" HorizontalAlignment="Left" VerticalAlignment="Top" Background="White" Grid.RowSpan="2">
        <TextBlock HorizontalAlignment="Left" Height="17" Margin="10,3,0,0" TextWrapping="Wrap" VerticalAlignment="Center" Width="88" FontSize="11"><Run Language="en-gb" Text="Input data"/></TextBlock>
    </Border>
<Controls:SequenceTrendingControl x:Name="sequenceControlStratigraphy" Grid.Column="1" Grid.Row="1" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="25, 12, 0, 0"/>
    <TextBlock Text="Sequence Stratigraphy" FontFamily="{StaticResource FontFamily}" Grid.Column="1" Grid.Row="1" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="48,20,0,0" FontSize="11"/>
    <Path Data="M2.4,62 L103.4,62" HorizontalAlignment="Left" Height="1" Margin="22,42,0,0" Stretch="Fill" Stroke="#A8A8A8" VerticalAlignment="Top" Grid.Column="1" Grid.Row="1"/>
    <Controls:FitButtonControl x:Name="fitButtonControlStratigraphy" Grid.Row="1" Grid.Column="1" VerticalAlignment="Top" HorizontalAlignment="Right" Margin="0,15,0,0"/>
    <Stratigraphy:StratiGraphControl x:Name="graphControlStatigraphy" Grid.Row="1" Grid.Column="1" Margin="5,49,5,5"  VerticalAlignment="Top" Grid.RowSpan="6" />

修改

enter image description here

第一张照片是我的笔记本电脑中的照片,你可以看到控件重叠

enter image description here

上面的图片是我桌面上显示的图片

现在我的问题是如何阻止事物的重叠,无论它在哪个屏幕上?

1 个答案:

答案 0 :(得分:0)

要在窗口大小更改时调整Grid大小,可以将Grid包装在ScrollViewer元素中。这会向您的内容添加滚动条而不是调整其大小。将VerticalScrollBarVisibilityHorizontalScrollBarVisibility设置为Auto只会在需要时显示滚动条。

<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
    <Grid Background="White" x:Name="GeologicalAnalysisUI">
        <!-- Your data -->
    </Grid>
</ScrollViewer>

除非ScrollViewer是具有特定尺寸设置的另一个控件的子代,否则仍然可以进行升级。 a(最大)宽度/高度在观察者本身上设置。