c# – 在调整子级大小时保持scrollviewer的相对滚动条偏移量

我有一个带有网格的滚动查看器作为孩子.我正在更改网格的宽度和高度属性显示不同的“缩放”级别.网格包含2行,其中包含许多图像列,大小相同.

但是,我希望滚动条的相对位置保持不变.在更改网格大小后,屏幕中心的任何内容仍应位于屏幕中央.

认“放大”视图:

private void SizeGrid()
{
    grid1.Width = (scrollViewer1.ViewportWidth / 2) * grid1.ColumnDeFinitions.Count;
    grid1.Height = (scrollViewer1.ViewportHeight / 2) * grid1.RowDeFinitions.Count;        
}

“缩小”观点:

private void scrollViewer1_KeyDown(object sender,KeyEventArgs e)
{
    if (e.KeyboardDevice.IsKeyDown(Key.Insert))
    {
        grid1.Width = (scrollViewer1.ViewportWidth / 2) * grid1.ColumnDeFinitions.Count / 5;
        grid1.Height = (scrollViewer1.ViewportHeight / 2) * grid1.RowDeFinitions.Count / 3;
    }
}

我做了什么……

如果我知道哪个列是关注的(我不想知道这个):

double shiftAmount = (scrollViewer1.ScrollableWidth / (grid1.ColumnDeFinitions.Count - columnsOnScreen));
scrollViewer1.ScrollToHorizontalOffset(column * shiftAmount);

如果我不确切知道他们正在看什么列,但我只是想保持相对位置……

double prevIoUsScrollRatio = scrollViewer1.HorizontalOffset / scrollViewer1.ScrollableWidth;
//resize grid...
scrollViewer1.ScrollToHorizontalOffset(prevIoUsScrollRatio * scrollViewer1.ScrollableWidth);

两种方法都不奏效.如果我以滚动条居中缩小,则滚动条将转到最右侧.任何的想法?

可以在上面找到一个最小的代码示例here加上scroll_KeyDown方法.

认缩放的屏幕截图:

缩小后的屏幕截图,不正确(海军蓝和粉红色方块远离屏幕):

缩小后的屏幕截图,它应该是什么样子:

解决方法

这是一种在放大或缩小时将内容保持在中心的解决方
//variables to store the offset values
    double relX;
    double relY;
    void scrollViewer1_ScrollChanged(object sender,ScrollChangedEventArgs e)
    {
        ScrollViewer scroll = sender as ScrollViewer;
        //see if the content size is changed
        if (e.ExtentWidthChange != 0 || e.ExtentHeightChange != 0)
        {
            //calculate and set accordingly
            scroll.ScrollToHorizontalOffset(CalculateOffset(e.ExtentWidth,e.ViewportWidth,scroll.ScrollableWidth,relX));
            scroll.ScrollToVerticalOffset(CalculateOffset(e.ExtentHeight,e.ViewportHeight,scroll.ScrollableHeight,relY));
        }
        else
        {
            //store the relative values if normal scroll
            relX = (e.HorizontalOffset + 0.5 * e.ViewportWidth) / e.ExtentWidth;
            relY = (e.VerticalOffset + 0.5 * e.ViewportHeight) / e.ExtentHeight;
        }
    }

    private static double CalculateOffset(double extent,double viewPort,double scrollWidth,double relBefore)
    {
        //calculate the new offset
        double offset = relBefore * extent - 0.5 * viewPort;
        //see if it is negative because of initial values
        if (offset < 0)
        {
            //center the content
            //this can be set to 0 if center by default is not needed
            offset = 0.5 * scrollWidth;
        }
        return offset;
    }

后面的想法是存储最后一个滚动位置,并在每次更改内容大小时使用它计算新的偏移量,这将使范围发生变化.

只需将ScrollViewer的事件ScrollChanged附加到构造函数等中的此事件处理程序,并将其余部分保留给它.

例如

scrollViewer1.ScrollChanged += scrollViewer1_ScrollChanged;

上述解决方案将确保将电网保持在中心位置,即使是第一次负载也是如此

中心内容的样本

放大了

缩小了

额外

我也尝试为同一个创建一个可附加的行为,因此您不需要连接事件,只需设置属性将启用或禁用该行为

namespace CSharpWPF
{
    public class AdvancedZooming : DependencyObject
    {
        public static bool GetKeepInCenter(DependencyObject obj)
        {
            return (bool)obj.GetValue(KeepInCenterProperty);
        }

        public static void SetKeepInCenter(DependencyObject obj,bool value)
        {
            obj.SetValue(KeepInCenterProperty,value);
        }

        // Using a DependencyProperty as the backing store for KeepInCenter.  This enables animation,styling,binding,etc...
        public static readonly DependencyProperty KeepInCenterProperty =
            DependencyProperty.Registerattached("KeepInCenter",typeof(bool),typeof(AdvancedZooming),new PropertyMetadata(false,OnKeepInCenterChanged));

        // Using a DependencyProperty as the backing store for Behavior.  This enables animation,etc...
        public static readonly DependencyProperty BehaviorProperty =
            DependencyProperty.Registerattached("Behavior",new PropertyMetadata(null));

        private static void OnKeepInCenterChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)
        {
            ScrollViewer scroll = d as ScrollViewer;

            if ((bool)e.NewValue)
            {
                //attach the behavior
                AdvancedZooming behavior = new AdvancedZooming();
                scroll.ScrollChanged += behavior.scroll_ScrollChanged;
                scroll.SetValue(BehaviorProperty,behavior);
            }
            else
            {
                //dettach the behavior
                AdvancedZooming behavior = scroll.GetValue(BehaviorProperty) as AdvancedZooming;
                if (behavior != null)
                    scroll.ScrollChanged -= behavior.scroll_ScrollChanged;
                scroll.SetValue(BehaviorProperty,null);
            }
        }

        //variables to store the offset values
        double relX;
        double relY;
        void scroll_ScrollChanged(object sender,ScrollChangedEventArgs e)
        {
            ScrollViewer scroll = sender as ScrollViewer;
            //see if the content size is changed
            if (e.ExtentWidthChange != 0 || e.ExtentHeightChange != 0)
            {
                //calculate and set accordingly
                scroll.ScrollToHorizontalOffset(CalculateOffset(e.ExtentWidth,relX));
                scroll.ScrollToVerticalOffset(CalculateOffset(e.ExtentHeight,relY));
            }
            else
            {
                //store the relative values if normal scroll
                relX = (e.HorizontalOffset + 0.5 * e.ViewportWidth) / e.ExtentWidth;
                relY = (e.VerticalOffset + 0.5 * e.ViewportHeight) / e.ExtentHeight;
            }
        }

        private static double CalculateOffset(double extent,double relBefore)
        {
            //calculate the new offset
            double offset = relBefore * extent - 0.5 * viewPort;
            //see if it is negative because of initial values
            if (offset < 0)
            {
                //center the content
                //this can be set to 0 if center by default is not needed
                offset = 0.5 * scrollWidth;
            }
            return offset;
        }
    }
}

启用行为

通过xaml

<ScrollViewer l:AdvancedZooming.KeepInCenter="True">

要么

<Style targettype="ScrollViewer" x:Key="zoomCenter">
    <Setter Property="l:AdvancedZooming.KeepInCenter"
            Value="True" />
</Style>

或者通过像

scrollViewer1.SetValue(AdvancedZooming.KeepInCenterProperty,true);

要么

AdvancedZooming.SetKeepInCenter(scrollViewer1,true);

通过样式或以编程方式设置属性l:AdvancedZooming.KeepInCenter =“True”内联,以便在任何scrollviewer上启用行为

l:在本例中指的是AdvancedZooming类的命名空间xmlns:l =“clr-namespace:CSharpWPF”

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


原文地址:http://msdn.microsoft.com/en-us/magazine/cc163791.aspx 原文发布日期: 9/19/2005 原文已经被 Microsoft 删除了,收集过程中发现很多文章图都不全,那是因为原文的图都不全,所以特收集完整全文。 目录 前言 CLR启动程序
前言 随着近些年微服务的流行,有越来越多的开发者和团队所采纳和使用,它的确提供了很多的优势也解决了很多的问题,但是我们也知道也并不是银弹,提供优势的同时它也给我们的开发人员和团队也带来了很多的挑战。 为了迎接或者采用这些新技术,开发团队需要更加注重一些流程或工具的使用,这样才能更好的适应这些新技术所
最近因为比较忙,好久没有写博客了,这篇主要给大家分享一下PLINQ中的分区。上一篇介绍了并行编程,这边详细介绍一下并行编程中的分区和自定义分区。 先做个假设,假设我们有一个200Mb的文本文件需要读取,怎么样才能做到最优的速度呢?对,很显然就是拆分,把文本文件拆分成很多个小文件,充分利用我们计算机中
在多核CPU在今天和不久的将来,计算机将拥有更多的内核,Microsoft为了利用这个硬件特性,于是在Visual Studio 2010 和 .NET Framework 4的发布及以上版本中,添加了并行编程这个新特性,我想它以后势必会改变我们的开发方式。 在以前或者说现在,我们在并行开发的时候可
c语言输入成绩怎么判断等级
字符型数据在内存中的存储形式是什么
c语言怎么求字符串的长度并输出
c语言函数的三种调用方式是什么
c语言中保留两位小数怎么表示
double的输入格式符是什么
长整型输出格式是什么
C语言中文件包含的命令关键字是什么
c程序如何编写x的y次方
c语言开根号代码是什么
c语言怎么进行字符串比较
c语言怎么进行强制类型转换
c语言运算符的优先级顺序是什么
c++用什么软件编程
中序遍历是怎么遍历的
h文件和c文件的关系是什么