Nested scrollviewers with datagrids

I have a window with the following contents:

<Grid> <ScrollViewer x:Name="Outer" CanContentScroll="True" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="2*" /> </Grid.RowDefinitions> <ScrollViewer x:Name="Inner1" CanContentScroll="True" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Auto"> <DataGrid HorizontalAlignment="Stretch" MinWidth="700" > <DataGrid.Columns> <DataGridCheckBoxColumn Header="Column A" /> <DataGridCheckBoxColumn Header="Column B" /> </DataGrid.Columns> </DataGrid> </ScrollViewer> <TextBox Width="500" Grid.Row="1" HorizontalAlignment="Left"/> <ScrollViewer x:Name="Inner2" Grid.Row="1" CanContentScroll="True" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Auto"> <DataGrid HorizontalAlignment="Stretch" > <DataGrid.Columns> <DataGridCheckBoxColumn Header="Column " /> <DataGridCheckBoxColumn Header="Column " /> </DataGrid.Columns> </DataGrid> </ScrollViewer> </Grid> </ScrollViewer> </Grid> 

My goal is to do the following: if the width of the contents of the window is less than the width of the window, an external horizontal scroll bar should be displayed. If I increase the column width of the data table - only the horizontal scroll bar for this datagrid should appear.

What blocks me:

When I increase the width of a datagrid column - it resizes its datagrid and the horizontal scroll bar of Outer , instead of " Inner1 ", which remains inactive.

In fact, in my application, I have a window with a frame control. A frame loading page with custom content and page size may be larger than the window size. Frame control is wrapped using ScrollViewer. The page contains some DataGrids wrapped in scrollviewers to display a horizontal scrollbar when changing the column width, even if there are no rows in the datagrid. Therefore, when I increase the width of the datagrid column, it resizes the datagrid -> datagrid resizes the entire page -> scrollviewer that appears in the wrapper frame. Is it possible to disable datagrid resizing while changing column width?

+4
source share
1 answer

It seems that since they are nested on the same grid line, they used a top-level scroll viewer for both. This is a fix, but not sure if this is what you want.

  <Window x:Class="NestedScrollViewer.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <ScrollViewer x:Name="Outer" CanContentScroll="True" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"> <Grid> <DataGrid HorizontalAlignment="Stretch" > <DataGrid.Columns> <DataGridCheckBoxColumn Header="Column A" /> <DataGridCheckBoxColumn Header="Column B" /> </DataGrid.Columns> </DataGrid> </Grid> </ScrollViewer> <ScrollViewer x:Name="Inner2" Grid.Row="1" CanContentScroll="false" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Auto"> <Grid> <DataGrid HorizontalAlignment="Stretch" > <DataGrid.Columns> <DataGridCheckBoxColumn Header="Column " /> <DataGridCheckBoxColumn Header="Column " /> </DataGrid.Columns> </DataGrid> </Grid> </ScrollViewer> </Grid> </Window> 
-one
source

Source: https://habr.com/ru/post/1497042/


All Articles