简体   繁体   中英

wpf custom control scrolling

I have just started working on custom control in wpf. I have created an controlling inheriting from the Frameworkelement and I am drawing the content. The content is is bigger than the height of the window. The scroll is not appearing even after placing the control in the scrollviewer.

Since you inherited from Framework Element and drawing your content using DrawingContext, the control may not measured properly. You should understand the Layout System of WPF. WPF followed two pass layout system . Measure and Arrange. In Measure Override, you need to tell the control how much size that need. In Arrange you need to place the control in proper rect.

The following case returns 300x300 pixels to the control. You may need to calculate the size based on your logic of rendering the content

    protected override Size MeasureOverride(Size availableSize)
    {
        return new Size(300, 300);
    }

Set:

ScrollViewer.VerticalScrollBarVisibility="Visible"

On your custom control. Also:

ScrollViewer.CanContentScroll="True"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM