WPF和放大器; MVVM:保存的ScrollViewer当前位置,并设置当重装 [英] WPF & MVVM: Save ScrollViewer Postion And Set When Reloading

查看:131
本文介绍了WPF和放大器; MVVM:保存的ScrollViewer当前位置,并设置当重装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个StackPannel一个ScrollViewer中。用户想要保存的ScrollViewer的位置,所以当应用程序被重新加载他们的数据StackPannel将展示他们之前所查看的项目。它无关,与被选中的项目,如果有的话,只是相对于StackPannel项目的ScrollViewer中的药水。
因此,如果StackPannel有50个项目和ScrollViewer中滚动,这样的StackPannel项20-25是可见的,我需要重新加载应用程序和向下滚动到该位置没有选择一个项目。
此外,我使用MVVM,我想通过设置在ViewModel代码太ScrollViewer中的位置。

I've got a ScrollViewer for a StackPannel. The users want the save the position of the ScrollViewer so when the application is re-loaded with their data the StackPannel will show the items they were viewing before. It has nothing to do with which items were selected, if any, merely the potion of the ScrollViewer in relation to the StackPannel items. So, if the StackPannel has 50 items and the ScrollViewer is scrolled so that items 20-25 of the StackPannel are visible I need to reload the application and scroll down to that position without selecting an item. Also, I'm using MVVM and I'd like to set the ScrollViewer position via the ViewModel code too.

推荐答案

下面的示例将在一个虚拟机存储所滚动偏移量和窗口(TestWindow)打开时加载它。你也应该存储和窗口的负载大小,因为它很可能会影响到滚动偏移量为好。如果你愿意,你可以在TestWindow后面移动代码到一个附加行为类

Below sample will store scroll offset in a VM and load it when window (TestWindow) opens. You should also store and load size of window since it will most likely affect scroll offset as well. If you want to you can move the code behind in TestWindow to an attached behavior class.

XAML:

<Window x:Class="ScrollTest.TestWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="TestWindow" Height="200" Width="300"
    Loaded="OnLoaded"
    Closing="OnClosing">
    <Grid>
        <ScrollViewer Name="_scroll"  VerticalScrollBarVisibility="Auto">
            <StackPanel>
                <Button Content="Click me" />
                <Button Content="Click me" />
                <Button Content="Click me" />
                <Button Content="Click me" />
                <Button Content="Click me" />
                <Button Content="Click me" />
                <Button Content="Click me" />
                <Button Content="Click me" />
                <Button Content="Click me" />
                <Button Content="Click me" />
                <Button Content="Click me" />
                <Button Content="Click me" />
                <Button Content="Click me" />
                <Button Content="Click me" />
                <Button Content="Click me" />
                <Button Content="Click me" />
            </StackPanel>
        </ScrollViewer>
    </Grid>
</Window>



后面的代码:

Code behind:

using System;
using System.ComponentModel;

using System.Windows;


namespace ScrollTest
{
    public partial class TestWindow : Window
    {
        public TestWindow()
        {
            InitializeComponent();
        }

        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            _scroll.ScrollToVerticalOffset((DataContext as VM).ScrollOffset);
        }

        private void OnClosing(object sender, CancelEventArgs e)
        {
            (DataContext as VM).ScrollOffset = _scroll.VerticalOffset;
        }
    }

    public class VM
    {
        public double ScrollOffset { get; set; }
    }
}



用法:

Usage:

private void OnOpenOpenTestWindow(object sender, RoutedEventArgs e)
{
    TestWindow testWindow = new TestWindow();
    testWindow.DataContext = _vm;
    testWindow.Show();
}

private VM _vm = new VM();

这篇关于WPF和放大器; MVVM:保存的ScrollViewer当前位置,并设置当重装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆