Is there a way to automagically make a canvas scro

2019-02-07 13:21发布

Been checking the web and this site, but couldn't come up with any descent results.

Is there a way to make a canvas in WPF show scrollbars on overflow ? Been trying the scrollviewer, but can't get it to work :(

Thanks in advance..

2条回答
趁早两清
2楼-- · 2019-02-07 13:46

The problem you're running into is that Canvas, unlike many WPF panels and containers, does not size to contents. That means if you add an element which goes outside the canvas boundaries it will not update it's size. Hence embedding a Canvas in a ScrollViewer will do no good unless you manually update the size of the Canvas.

It sounds like what you want is a Canvas which supports size to contents. This blog entry has exactly that control.

http://themechanicalbride.blogspot.com/2008/11/auto-sizing-canvas-for-silverlight-and.html

查看更多
爷、活的狠高调
3楼-- · 2019-02-07 14:03

I took a different approach and abandoned the Canvas for Grid. The Canvas is more performant but for my purposes at least I haven't noticed a difference. The grid can mimic the behavior of canvas by doing the following.

Create a single row,single column grid. Set the HorizontalAlignment to Left Set the VerticalAlignment to Top Use Margin "x,y,0,0" to set the position.

Bam..works just like canvas and it works great in a Scrollviewer.

查看更多
登录 后发表回答