如何在 WPF Canvas 上绘制滚动条

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2828942/
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-08 21:45:28  来源:igfitidea点击:

How to draw scrollbars on WPF Canvas

wpfcanvasscrollbars

提问by Scooby

I am trying to create a canvas with scroll bars. Can anyone help me give some ideas on how to do this? I have already tried using grid of 1 row and 1 column but due to certain constraints I want to use canvas.

我正在尝试创建一个带有滚动条的画布。谁能帮我就如何做到这一点提出一些想法?我已经尝试过使用 1 行 1 列的网格,但由于某些限制,我想使用画布。

Thanks in advance!

提前致谢!

回答by Ben Collier

You could put the canvas inside of a scrollviewer. I tried this quick test and it allowed me to scroll through the contents of the canvas.

您可以将画布放在滚动查看器中。我尝试了这个快速测试,它允许我滚动画布的内容。

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

edit: Here is an example where the scroll-bars show up only when needed, and it changes dynamically as the canvas size changes.

编辑:这是一个示例,其中滚动条仅在需要时显示,并且随着画布大小的变化而动态变化。

    <Button Content="Change Canvas Size" Click="ChangeCanvasSize_Click"/>
<ScrollViewer Height="100" Width="200" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
    <Canvas x:Name="TestCanvas">
            <TextBlock Text="Test Test"/>
    </Canvas>
 </ScrollViewer>

Changing canvas size with button click:

单击按钮更改画布大小:

    private void ChangeCanvasSize_Click(object sender, RoutedEventArgs e)
    {
        TestCanvas.Width = 600;
        TestCanvas.Height = 600;
    }

In this example, I start out with no scroll-bars and when I click the button to expand the canvas, scroll-bars appear.

在本例中,我开始时没有滚动条,当我单击按钮展开画布时,会出现滚动条。

回答by Scooby

Ok after working with it for sometime I figured out a way. Create a XAML like this

好的,在使用它一段时间后,我想出了一种方法。创建这样的 XAML

<ScrollViewer>
 <Grid x:Name="drawingGrid" SizeChanged="drawingGrid_SizeChanged">
<Canvas Name="drawingCanvas"> /<Canvas>
</Grid>
</ScrollViewer>

On windowLoad function set the canvas height/width equal to grid height/width. Update the canvas ht/wd:

在 windowLoad 函数上设置画布高度/宽度等于网格高度/宽度。更新画布 ht/wd:

  1. when grid size changes, due to mininmize/maximize.
  2. dragging an element beyond the boundaries of canvas or creating a new element too close the edge of canvas

    double dHeight = 220;
    if (drawingCanvas.Height < CurrentPosition.Y + dHeight)
    {
        // increase canvas height
        drawingCanvas.Height += (2 * dHeight);
    }
    
  1. 当网格大小发生变化时,由于最小化/最大化。
  2. 将元素拖出画布边界或创建新元素太靠近画布边缘

    double dHeight = 220;
    if (drawingCanvas.Height < CurrentPosition.Y + dHeight)
    {
        // increase canvas height
        drawingCanvas.Height += (2 * dHeight);
    }
    

Hope this is of some help. Please share if anyone has any better idea or suggestions to improve this.

希望这个对你有帮助。如果有人有更好的想法或建议来改进这一点,请分享。

回答by ArchAngel

By combining Mario-sannum's answer and your question then I've made a solution that should work fine in most cases..

通过将 Mario-sannum 的回答和您的问题结合起来,我制定了一个在大多数情况下应该可以正常工作的解决方案。

<ScrollViewer>
<Grid x:Name="drawingGrid" SizeChanged="drawingGrid_SizeChanged">
<Canvas Name="c">
<TextBlock x:Name="draw_Text" Text="Test Test"/>
</<Canvas>
</Grid>
</ScrollViewer>


void drawingGrid_SizeChanged(object sender, SizeChangedEventArgs e)
{
      try { c.Height = draw_Text.ActualHeight; } catch { }
      try { c.Width = draw_Text.ActualWidth; } catch { }
}

That should resize the Canvas so the scrollviewer can scroll...

这应该调整画布的大小,以便滚动查看器可以滚动...