Silverlight ScrollViewer не обновляется после увеличения - PullRequest
0 голосов
/ 14 февраля 2012

У меня есть сетка 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 голосов
/ 25 марта 2012

Я сейчас работаю над тем же вопросом,

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