简体   繁体   English

wpf自定义控件滚动

[英]wpf custom control scrolling

I have just started working on custom control in wpf. 我刚开始研究wpf中的自定义控件。 I have created an controlling inheriting from the Frameworkelement and I am drawing the content. 我创建了一个继承自Frameworkelement的控件,我正在绘制内容。 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. 由于您从Framework Element继承并使用DrawingContext绘制内容,因此可能无法正确测量控件。 You should understand the Layout System of WPF. 您应该了解WPF的布局系统。 WPF followed two pass layout system . WPF遵循两个布局系统 Measure and Arrange. 衡量和安排。 In Measure Override, you need to tell the control how much size that need. 在Measure Override中,您需要告诉控件需要多少大小。 In Arrange you need to place the control in proper rect. 在安排中,您需要将控件放在正确的矩形中。

The following case returns 300x300 pixels to the control. 以下情况向控件返回300x300像素。 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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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