简体   繁体   English

窗口尺寸减小时的滚动条或ScrollViewer

[英]Scrollbar or ScrollViewer when windows sizes reduces

I want to know if there is a way to not size the width and height of a grid I have but when I reduce the size of a window if my graph becomes a certain width/height then the corresponding scrollviewer appear, is it possible to do in XAML or do I need to do this in code behind? 我想知道是否可以不对网格的宽度和高度进行大小调整,但是当我减小窗口的大小(如果图形变为一定的宽度/高度)时,会出现相应的scrollviewer,是否可以XAML中还是我需要在后面的代码中执行此操作? So I want my Controls to have widths and if the window gets below them then I want to be able to enable the scroll viewers that I have 因此,我希望控件具有宽度,并且如果窗口位于其下方,那么我希望能够启用我拥有的滚动查看器

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"/>

As you requested in the comments an example for the second approach. 如您在评论中所要求的,第二种方法的示例。 It's only doing the scrollviewer logic so I do not know if this is enough in your case but that is what you ask for so here it is: 它只是在做scrollviewer逻辑,所以我不知道这在您的情况下是否足够,但这就是您要的,所以这里是:

This code will hide/show vertical/horizontal scrollbar if window size is below/above given values 如果窗口大小小于/大于给定值,则此代码将隐藏/显示垂直/水平滚动条

XAML - put this in your window XAML-将其放在您的窗口中

SizeChanged="Window_SizeChanged"    

Code Behind 背后的代码

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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM