2012-02-13 86 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(); 
}