当窗口大小减小时,滚动条或滚动查看器

本文关键字:滚动 滚动条 窗口大小 小时 | 更新日期: 2023-09-27 18:22:35

我想知道是否有一种方法可以不调整网格的宽度和高度,但当我缩小窗口的大小时,如果我的图形变成某个宽度/高度,则会出现相应的滚动查看器,这可以在XAML中实现吗,还是需要在代码隐藏中实现?所以我希望我的控件有宽度,如果窗口低于它们,那么我希望能够启用我有的滚动查看器

XAML

<ScrollViewer x:Name="MyScrollViewer" VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Disabled">
    <Grid Background="White" x:Name="OuterGrid">
        <Grid.RowDefinitions>
            <RowDefinition Height="70"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
<Grid Background="White" Grid.Row="1" x:Name="UIWindow" Grid.ColumnSpan="2">
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="1.2*"/>
                <ColumnDefinition Width="1.2*"/>
                <ColumnDefinition Width="1.5*"/>
                <ColumnDefinition Width="1.5*"/>
            </Grid.ColumnDefinitions>
<TextBlock HorizontalAlignment="Left" Margin="25,3,0,2" Height="17" VerticalAlignment="Top" Width="70" Text="Facies" Grid.Column="0" Grid.Row="0" FontFamily="{StaticResource FontFamily}"/>
            <Path Data="M2.4,62 L103.4,62" HorizontalAlignment="Left" Height="1" Margin="5,20,0,0" Stretch="Fill" Stroke="#A8A8A8" VerticalAlignment="Top" Width="200" Grid.Column="0" Grid.Row="0"/>
            <Controls:FaciesControl x:Name="FaciesFilter" HorizontalAlignment="Left" Margin="10,25,0,0" VerticalAlignment="Top" Height="300" Width="195" Grid.Row="0" Grid.Column="0" Grid.RowSpan="3"/>
            <TextBlock Grid.Column="0" Grid.Row="2" Text="Zone Legend:" Width="150" Height="15" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="10" FontFamily="{StaticResource FontFamily}"/>
            <Path Data="M2.4,62 L103.4,62" HorizontalAlignment="Left" Height="1" Margin="5,27,0,0" Stretch="Fill" Stroke="#A8A8A8" VerticalAlignment="Top" Width="200" Grid.Column="0" Grid.Row="2"/>
            <Controls:LegendControl x:Name="LegendFilter" HorizontalAlignment="Left" Height="450" Margin="5,50,0,0" Grid.Row="2" Grid.Column="0" VerticalAlignment="Top" Width="224" Grid.RowSpan="4"/>
            <TextBlock Text="Sequence Stratigraphy" FontFamily="{StaticResource FontFamily}" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="5,5,0,0" Grid.Column="1" Grid.Row="0"/>
            <Path Data="M2.4,62 L103.4,62" HorizontalAlignment="Left" Height="1" Margin="5,20,0,0" Stretch="Fill" Stroke="#A8A8A8" VerticalAlignment="Top" Width="355" Grid.Column="1" Grid.Row="0"/>
            <Stratigraphy:StratiGraphControl x:Name="stratiGraphControl" Grid.Row="0" Grid.Column="1" Grid.RowSpan="4" Margin="0,40,40,10"/>

当窗口大小减小时,滚动条或滚动查看器

正如您在评论中所要求的,第二种方法的示例。它只是在做滚动查看器逻辑,所以我不知道这在你的情况下是否足够,但这正是你所要求的,所以这里是:

如果窗口大小低于/高于给定值,此代码将隐藏/显示垂直/水平滚动条

XAML-将其放入您的窗口

SizeChanged="Window_SizeChanged"    

代码隐藏

private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
{
    if(e.NewSize.Width <= 500)
    {
        MyScrollViewer.HorizontalScrollBarVisibility = ScrollBarVisibility.Visible;
    }
    else
    {
        MyScrollViewer.HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled;
    }
    if (e.NewSize.Height <= 500)
    {
        MyScrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;
    }
    else
    {
        MyScrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Disabled;
    }
}