2

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

2 Answers 2

2

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>
0

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.

1
  • Hi Kent. No I haven't overridden that method, only the one I said (OnRender). My custom control is a little bit more complicated than explained, so posting the code will be a little confusing. Commented Jan 16, 2011 at 19:19

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.