Wednesday, August 22, 2012

WPF ScrollViewer Control Example

There are two controls that enables scrolling in WPF: ScrollBar and ScrollViewer. The ScrollViewer encapsulates both vertical and horizontal ScrollBar and a container that contains all the visible elements inside scrollable area. I'll show a ScrollBar example based on code described in my previous post.
WPF ScrollViewer example

Let's see the XAML:
<ScrollViewer HorizontalScrollBarVisibility="Auto" 
    <Expander Name="MyExpander" Margin="10,10" >
            <DataGrid Name="dataGrid1"
                ItemsSource="{Binding Employees}" />
            <Style TargetType="Expander">
                <Setter Property="IsExpanded" Value="False" />
                <Setter Property="Header" Value="Click to See More" />
                    <DataTrigger Binding="{Binding IsExpanded,RelativeSource={RelativeSource Self}}"                                 Value="True">
                        <Setter Property="Header" Value="Click to See Less" />
By specifying HorizontalScrollBarVisibility and VerticalScrollBarVisibility as "Auto" we achieve that ScrollBar becomes visible only when content becomes larger than window frame.
Download the source code (Visual Studio 2010 project).