wpf 滚动查看器和画布

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30504869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:09:48  来源:igfitidea点击:

Scrollviewer & Canvas

wpfxamlscrollviewer

提问by Marshal

I am trying to load an image within a canvas such that, if the size of image overflows the canvas, the scroll bars should get activated (MS Paint style)

我试图在画布中加载图像,如果图像的大小溢出画布,滚动条应该被激活(MS Paint 样式)

<Window>
   <ScrollViewer>
        <Canvas  ScrollViewer.HorizontalScrollBarVisibility="Visible" 
                 ScrollViewer.VerticalScrollBarVisibility="Visible">
            <Image Source="SampleImage.jpg" />
        </Canvas>
   </ScrollViewer>
 </Window>
  • Now as Canvasis stretched to Window's size, scroll-bars won't appear as Canvasis not actually overflowing out of the Window.
  • Secondly, as the Imageis much bigger than the Canvas, it is getting clipped at the bounds of Canvas, so ScrollViewerdoesn't think that its content: Canvasis actually overflowing.
  • 现在随着Canvas拉伸到Window的大小,滚动条不会出现,因为Canvas实际上并没有从Window.
  • 其次,由于Image比 大得多Canvas,它在 的边界处被剪裁Canvas,所以ScrollViewer不认为它的内容:Canvas实际上是溢出。

It happens a lot of time with StackPanels too, that even though the bound data has tens of rows, but still the scrollbars don't get activated. Sometimes scrollviewers appear as mystery to me.

StackPanels 也经常发生这种情况,即使绑定数据有几十行,但滚动条仍然没有被激活。有时滚动查看器对我来说似乎很神秘。

So, what should be the basic logic kept in mind when using ScrollViewercontrol.

那么,在使用ScrollViewer控件时应该记住的基本逻辑是什么。

Thank you.

谢谢你。

Edit:Just edited the question title, so that whosoever has problem with canvas can get this question easily in search.

编辑:刚刚编辑了问题标题,以便任何对画布有问题的人都可以在搜索中轻松找到此问题。

回答by Liero

From MSDN:Canvas is the only panel element that has no inherent layout characteristics. A Canvas has default Height and Width properties of zero, unless it is the child of an element that automatically sizes its child elements. Child elements of a Canvas are never resized, they are just positioned at their designated coordinates. This provides flexibility for situations in which inherent sizing constraints or alignment are not needed or wanted. For cases in which you want child content to be automatically resized and aligned, it is usually best to use a Grid element.

来自 MSDN:Canvas 是唯一没有固有布局特征的面板元素。Canvas 的默认 Height 和 Width 属性为零,除非它是自动调整其子元素大小的元素的子元素。Canvas 的子元素永远不会调整大小,它们只是定位在指定的坐标处。这为不需要或不需要固有大小限制或对齐的情况提供了灵活性。对于您希望子内容自动调整大小和对齐的情况,通常最好使用 Grid 元素。

Hovever, you can set Canvas Height and Width explicitely:

但是,您可以明确地设置画布高度和宽度:

<ScrollViewer Height="100" Width="200">
    <Canvas Height="400" Width="400">
            //Content here
    </Canvas>
</ScrollViewer>