Silverlight ScrollViewer 缩放后未更新

发布于 2025-01-05 00:23:33 字数 988 浏览 1 评论 0原文

我有一个 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>

I have a Silverlight grid with a bunch of content in it (rectangles, textBlocks, etc.) which represents content in a room. Because it gets pretty complex, I decided I needed an ability to "zoom-in" on the grid. I found some good code to do that, but the problem is that after zooming the grids associated ScrollViewer doesn't scroll the full distance down or to the right. How can I force it to update so that I can scroll to the bottom and all the way to the right?

If it helps, here's the code to permit zooming of my Grid:

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;

and here is the XAML that shows the grid and the scroll viewer

    <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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

年华零落成诗 2025-01-12 00:23:33

我现在正在研究同样的问题,

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

I'm working on the same issue now,

the ScrollViewer is affected only by the change in Width or Height,
so to fix the problem, you have to do as the following:

suppose we got the Grid or Canvas named (ZoomCanvas)

in the code behind:

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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文