Ryan Archibald
Ryan Archibald

Reputation: 215

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? 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

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

Upvotes: 1

Views: 2012

Answers (1)

kirotab
kirotab

Reputation: 1306

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:

This code will hide/show vertical/horizontal scrollbar if window size is below/above given values

XAML - put this in your window

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

Upvotes: 3

Related Questions