2012-02-13 80 views
0

我有一个Silverlight网格,里面有一堆内容(矩形,textBlocks等),它代表了房间中的内容。因为它变得非常复杂,我决定我需要能够在网格上“放大”。我发现了一些很好的代码来做到这一点,但问题是,缩放相关的网格后,ScrollViewer不会向下或向右滚动全部距离。我如何强制更新,以便我可以滚动到底部并一路向右?Silverlight 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> 

回答

0

我的工作同样的问题现在,

ScrollViewer仅受宽度或高度的更改影响, 所以要解决此问题,您必须按以下方式执行操作:

假设我们有一个名为(ZoomCanvas)

在后面的代码电网或帆布:

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(); 
}