Vishnu Babu Vishnu Babu - 3 months ago 30
C# Question

Single scrollbar for multiple DataGrids on a grid WPF

I have two DataGrids on the grid in which one DataGrid is placed exactly below the other, both these DataGrids shares the same column headers what i want to do is that i want to use a single scroll bar (Horizontal Scroll bar) for both these DataGrids . How can i do this ??

<Grid>
<DataGrid Name="StatusGrid"
Height="Auto"
Width="Auto"
AutoGenerateColumns="False"
CanUserAddRows="False"
CanUserReorderColumns="False" />

<DataGrid AutoGenerateColumns="False"
Height="auto"
Width="auto"
Margin="0,73,0,10"
Name="heatMap"
IsReadOnly="True"
CanUserReorderColumns="False"
CanUserSortColumns="False"
ScrollViewer.CanContentScroll="True"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
ItemsSource="{Binding HeatMapCollection}"
Background="AliceBlue" />
</Grid>

Answer

Try using the ScrollViewer like

<ScrollViewer CanContentScroll="True"
              HorizontalScrollBarVisibility="Visible"
              VerticalScrollBarVisibility="Visible">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <DataGrid Grid.Row="0"
                  Name="StatusGrid"
                  Height="Auto"
                  Width="Auto"
                  AutoGenerateColumns="False"
                  CanUserAddRows="False"
                  CanUserReorderColumns="False" />

        <DataGrid Grid.Row="1"
                  AutoGenerateColumns="False"
                  Width="1000"
                  Margin="0,73,0,10"
                  Name="heatMap"
                  IsReadOnly="True"
                  CanUserReorderColumns="False"
                  CanUserSortColumns="False"
                  ItemsSource="{Binding HeatMapCollection}"
                  Background="AliceBlue" />
    </Grid>
</ScrollViewer>