Use ScrollViewer inside another Scrollviewer

后端 未结 2 1351
星月不相逢
星月不相逢 2020-12-19 14:35

The structure of my wpf application is like:


   
       
            

相关标签:
2条回答
  • 2020-12-19 14:47

    You might want to place the DataGrid outside the Scrollviewer then. Therefore you can set two Grid.Rows, and let it part the available size for you (default would be "equal" or if you use only two rows, 50%-50%). So if the DataGrid exceeds its available size it will use its own ScrollViewer. If the content below is exceeding the size, it will use your manually placed ScrollViewer. At least, that is how i would solved it based on your initial input. Feel free to add code / comments so we might find a better way.

    If you place two ScrollViewers plus a datagrid inside each other you will be forced to start passing out concrete Height or Width values if you want that kind of fine grained control.

    0 讨论(0)
  • 2020-12-19 15:00

    You need to set a height on the inner ScrollViewer, otherwise it'll stretch as much as it needs based on it's content's size.

    <Window x:Name="RootWindow">
        <ScrollViewer>
            <Grid Height="{Binding ElementName=RootWindow, Path=ActualHeight}">
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                    <RowDefinition Height="2*" />
                </Grid.RowDefinitions>
    
                <ScrollViewer Grid.Row="1">
                    <DataGrid />
                </ScrollViewer>
            </Grid>
        </ScrollViewer>
    </Window>
    

    Also, the DataGrid has built-in properties for it's own ScrollBars which you can use instead of wrapping the DataGrid in a ScrollViewer. This will scroll the data and always leave the headers visible, instead of scrolling the entire datagrid.

    <DataGrid HorizontalScrollBarVisibility="Auto" 
              VerticalScrollBarVisibility="Auto" />
    
    0 讨论(0)
提交回复
热议问题