Silverlight ScrollViewer在缩放后不会更新

时间:2012-02-13 21:12:13

标签: zoom scrollviewer

我有一个Silverlight网格,里面有一堆内容(矩形,textBlocks等),代表一个房间里的内容。因为它变得非常复杂,我认为我需要一种能够“放大”网格的能力。我找到了一些很好的代码来做到这一点,但问题是在缩放相关的网格ScrollViewer后不会向下或向右滚动整个距离。如何强制它更新,以便我可以滚动到底部并一直向右?

如果有帮助,这里是允许缩放网格的代码:

var style = new Style(typeof(Grid));
var scale = new ScaleTransform();
scale.CenterX = .5;
scale.CenterY =.5;
scale.ScaleX = Scale;
scale.ScaleY = Scale;
var rs = new Setter();
rs.Property = DataGridCell.RenderTransformProperty;
rs.Value = scale;
style.Setters.Add(rs);
OtdrPatchLocationGrid.Style = style;

这里是显示网格和滚动查看器的XAML

    <ScrollViewer Name="scViewer"  Grid.Row="1" Visibility="Visible"  VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible">
        <Grid x:Name="OtdrPatchLocationGrid" MinHeight="350"  VerticalAlignment="Stretch"  Background="Yellow" Grid.Row="1" Grid.Column="0" Margin="0" MouseDown="OtdrRackViewer_MouseDown">

        </Grid>
    </ScrollViewer>

1 个答案:

答案 0 :(得分:0)

我现在正在处理同样的问题,

ScrollViewer仅受宽度或高度变化的影响, 所以要解决这个问题,你必须做如下的事情:

假设我们得到了名为(ZoomCanvas)的Grid或Canvas

在后面的代码中:

double initialCanvasHeight;
double initialCanvasWidth;
public void MainPage() //As the Constructor
{
 initialCanvasHeight = ZoomCanvas.Height;
 initialCanvasWidth = ZoomCanvas.Width;
}

ZoomCanvas_MouseWheel(object sender, MouseWheelEventArgs e)
{

/*Assuming you have the scaling code here and the object CanvasScale is used to scale the canvas*/

 foreach (var node in ZoomCanvas)
            {
                var nodeTop = Canvas.GetTop(node);
                var nodeLeft = Canvas.GetLeft(node);
                if(mostTopValue < nodeTop)
                    mostTopValue = nodeTop;
                if(mostLeftValue < nodeLeft)
                    mostLeftValue = nodeLeft;
                var desiredHeight = (mostTopValue + NodeHeight)*canvasScale.ScaleY;
                var desiredWidth = (mostLeftValue + NodeWidth) * canvasScale.ScaleX;
                if (desiredHeight > canvasInitialHeight)
                {
                    while (heightToIncrease < desiredHeight)
                        heightToIncrease += 10;
                    ZoomCanvas.Height = heightToIncrease;
                }
                else
                    while (ZoomCanvas.Height > canvasInitialHeight)
                        ZoomCanvas.Height -= 10;
                if (desiredWidth > canvasInitialWidth)
                {
                    while (widthToIncrease < desiredWidth)
                        widthToIncrease += 10;
                    ZoomCanvas.Width = widthToIncrease;
                }
                else while (ZoomCanvas.Height > canvasInitialHeight)
                    ZoomCanvas.Width -= 10;
            }
            scrollViewer.UpdateLayout();
}