如何正确地将canvas绑定到scrollViewer

时间:2015-12-08 12:43:53

标签: c# wpf xaml canvas scrollviewer

我想将canvas组件与scrollViewer绑定。我有一个放大和缩小canvas的功能,我希望使用符合scrollViewer实际尺寸的canvas添加滚动条。

我的画布的放大/缩小功能:

private void canvasShopMap_MouseWheel(object sender, MouseWheelEventArgs e)
        {

            if (e.Delta > 0)
            {
                scaleTransform.ScaleX += scaleRate;
                scaleTransform.ScaleY += scaleRate;

            }
            else
            {
                scaleTransform.ScaleX -= scaleRate;
                scaleTransform.ScaleY -= scaleRate;

            }

            canvasShopMap.RenderTransform = scaleTransform;
        } 

我尝试做的XAML代码(是的,我知道scrollViewer在这里是canvas,但我尝试了上面的函数用于scrollViewer并且它没有工作; /):

<Canvas x:Name="canvasShopMap" HorizontalAlignment="Left" Height="500" VerticalAlignment="Top" Width="690" MouseLeftButtonDown="canvasShopMap_MouseLeftButtonDown" MouseWheel="canvasShopMap_MouseWheel" Margin="272,139,0,0">
            <ScrollViewer x:Name="scrollViewerForCanvas" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto"
                          Height="{Binding Parent.ActualHeight , Mode=OneWay, RelativeSource={RelativeSource Self}}"
                          Width="{Binding Parent.ActualWidth , Mode=OneWay, RelativeSource={RelativeSource Self}}" MouseWheel="scrollViewerForCanvas_MouseWheel">               
            </ScrollViewer>
        </Canvas>

有什么建议吗?

0 个答案:

没有答案