开发者

Use ScrollViewer inside another Scrollviewer

开发者 https://www.devze.com 2023-04-08 02:18 出处:网络
The structure of my wpf application is like: <Scrollviewer> <Grid>开发者_如何学运维;

The structure of my wpf application is like:

<Scrollviewer>
   <Grid>开发者_如何学运维;
       <Scrollviewer>
            <DataGrid>

My Goal is, if the DataGrid exceeds the height of the screen to use it's own Scrollviewer. At the Moment only the outside ScrollViewer is used and so i have to scroll the whole Grid.

Can someone please tell me how to do this?


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


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号