Matthew Bill
Matthew Bill

Reputation: 203

WPF - custom control ScrollViewer

I have created a custom control that simply draws a grid of squares on the screen. To do this it overrides the OnRender method and draws rectangles.

I have added this custom control to a WPF window. However when I resize the window, part of the custom control is hidden. What I want to happen is a scroll bar appears, however after adding a scroll viewer it hasn't done anything.

I have read elsewhere I should implementent IScrollInfo, but that seems like a lot of effort to do something quite simple.

If anyone could help me out it would be greatly appreciated.

Many thanks,

Matt

Upvotes: 2

Views: 2888

Answers (2)

Kent Boogaart
Kent Boogaart

Reputation: 178660

The ScrollViewer will use its child's DesiredSize as a determinant for whether scrolling is necessary. Does your custom control override Measure()? Posting the code for your custom control will likely be required to help further.

Upvotes: 0

Rick Sladkey
Rick Sladkey

Reputation: 34240

A ScrollViewer can scroll any arbitrary content so you don't need to implement IScrollInfo unless you want to support logical scrolling, i.e. by lines instead of by pixels.

Unless your custom control implements MeasureOverride it won't participate in the measure phase of layout and a ScrollViewer won't know big you want the scrollable region to be.

Here is a comple XAML-only example of a scrollable Grid with a graph paper background:

<DockPanel>
    <ScrollViewer Height="200" Width="250" HorizontalScrollBarVisibility="Visible">
        <Grid Height="400" Width="400">
            <Grid.Background>
                <DrawingBrush x:Name="GridBrush" 
                    Viewport="0,0,10,10" ViewportUnits="Absolute" TileMode="Tile">
                    <DrawingBrush.Drawing>
                        <DrawingGroup>
                            <GeometryDrawing Brush="#CCCCFF">
                                <GeometryDrawing.Geometry>
                                    <RectangleGeometry Rect="0,0 10,1" />
                                </GeometryDrawing.Geometry>
                            </GeometryDrawing>
                            <GeometryDrawing Brush="#CCCCFF">
                                <GeometryDrawing.Geometry>
                                    <RectangleGeometry Rect="0,0 1,10" />
                                </GeometryDrawing.Geometry>
                            </GeometryDrawing>
                        </DrawingGroup>
                    </DrawingBrush.Drawing>
                </DrawingBrush>
            </Grid.Background>
        </Grid>
    </ScrollViewer>
</DockPanel>

Upvotes: 2

Related Questions