WPF Canvas ScrollViewer [英] WPF Canvas ScrollViewer
问题描述
我正在尝试将画布控件放入滚动查看器。 canvas控件是动态的,这意味着我在运行时使用c#代码创建子代。
Hi,
I am trying to put a canvas control into a scrollviewer. The canvas control is dynamic, that means i am creating their children in c# code during runtime.
<ScrollViewer Grid.Row="1">
<Canvas Name="cvHeats" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
</ScrollViewer>
我的问题是,滚动条显示,但它们被禁用。如何使用动态内容使得scrollviewer适用于具有动态内容的画布?
提前致谢,
Manu
My problem is, that the scrollbars are displayed, but they are disabled. How
can i make the scrollviewer work for a canvas with dynamic content?
Thanks in advance,
Manu
推荐答案
此链接可以帮助您:
stackoverflow.com/questions/3061400/scrollbars-are-not-visible-after-changing-positions-of-controls-inside-a- canvas
尝试给
Canvas
一个大小。类似于:
<ScrollViewer Grid.Row="1">
<Canvas Name="cvHeats" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
Width="1000" Height="1000" />
</ScrollViewer>
您可以按如下方式在xaml中设置控件:
You can set your controls up in xaml as follows:
<scrollviewer scrollviewer.horizontalscrollbarvisibility="Auto">
ScrollViewer.VerticalScrollBarVisibility="Auto">
<canvas x:name="canMain" xmlns:x="#unknown">
<image x:name="imgMain" />
</canvas>
</scrollviewer>
然后订阅内部控件大小更改事件。在我的情况下,我正在使用图像,但您可以根据自己的需要更换它。
Then subscribe to the inner controls size changed event. In my case I'm using a image but you can replace it what ever suits your needs.
imgMain.SizeChanged += ImgMain_SizeChanged;
然后将画布的宽度和高度设置为图像的新尺寸。
Then in the set the width and height of your canvas to the new sizes of you image.
private void ImgMain_SizeChanged(object sender, SizeChangedEventArgs e)
{
canMain.Height = e.NewSize.Height;
canMain.Width = e.NewSize.Width;
}
这篇关于WPF Canvas ScrollViewer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!