Give the WPF Grid the ScrollViewer width minus the scrollbar

I have UserFontrol WPF with two grids on top of each other. The bottom grid is in the ScrollViewer. The idea is for the first grid to be the header of the second grid. However, I have problems with column widths. Both grids should occupy all the space that they can (window width), but the upper grid should, of course, be slightly less wide, because there is a scroll bar to the right of the lower grid.

This is a simplified version of Xaml:

<UserControl>
    <DockPanel>
        <Grid DockPanel.Dock="Top">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
        </Grid>
        <ScrollViewer>
            <Grid>
                <Grid.ColumnDefintions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefintions>
            </Grid>
        </ScrollViewer>
    </DockPanel>
</UserControl>

This is really great, except that the rightmost top grid column extends to the scroll bar that I want to avoid.

: . , /, , . SharedSizeGroups, , , ( ).

+3
2

. Grid a Width:

Width="{Binding ElementName=BottomGrid, Path=ActualWidth}"
+1
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Name="sv">
    <Grid MinWidth="{Binding ElementName=sv, Path=ViewportWidth}" MinHeight="{Binding ElementName=sv, Path=ViewportHeight}" />
</ScrollViewer>

ScrollViewer . : ScrollViewer :

(, ) ( , Width MinWidth).

ViewportWidth ActualWidth , .

+5

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


All Articles