WPF & MVVM: 保存位置 ScrollViewer 重新启动时安装它

我有 ScrollViewer 为了 StackPannel. 用户想要保存位置 ScrollViewer, 因此,当应用程序重新加载数据时, StackPannel 显示他们以前查看的物品。 它与选择的物品无关,如果有的话,只是一个药水 ScrollViewer 向 StackPannel 主题。
因此,如果 StackPannel 它有 50 元素,A。 ScrollViewer 滚动如此元素 20-25 的 StackPannel 您可以看到,我需要重新启动应用程序并在不选择项目的情况下向下滚动到此位置。
另外,我使用 MVVM 我也想建立一个位置 ScrollViewer 用代码 ViewModel.
已邀请:

知食

赞同来自:

下面的示例将保存滚动偏移 VM 并在窗户打开时加载它 /TestWindow/. 您还必须存储并加载窗口大小,因为它可能也会影响滚动偏移。 如果需要,您可以将代码移回 TestWindow 在附加的行为类别中。

XAML:


<window closing="OnClosing" height="200" loaded="OnLoaded" title="TestWindow" width="300" x:class="ScrollTest.TestWindow" xmlns="[url=http://schemas.microsoft.com/winfx/2006/xaml/presentation"]http://schemas.microsoft.com/w ... ot%3B[/url] xmlns:x="[url=http://schemas.microsoft.com/winfx/2006/xaml">]http://schemas.microsoft.com/w ... gt%3B[/url]
<grid>
<scrollviewer name="_scroll" verticalscrollbarvisibility="Auto">
<stackpanel>
<button content="Click me"></button>
<button content="Click me"></button>
<button content="Click me"></button>
<button content="Click me"></button>
<button content="Click me"></button>
<button content="Click me"></button>
<button content="Click me"></button>
<button content="Click me"></button>
<button content="Click me"></button>
<button content="Click me"></button>
<button content="Click me"></button>
<button content="Click me"></button>
<button content="Click me"></button>
<button content="Click me"></button>
<button content="Click me"></button>
<button content="Click me"></button>
</stackpanel>
</scrollviewer>
</grid>
</window>


对于代码 :


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


使用:


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

private VM _vm = new VM//;

涵秋

赞同来自:

您还可以设置位置。 ScrollViewer, 打电话
方法 ScrollToVerticalOffset.


contentScrollViewer.ScrollToVerticalOffset/50/;

三叔

赞同来自:

您查看了使用该物业的可能性 ScrollViewer.ScrollInfo? 它具有表示每个滚动条偏移量的元素 /
http://msdn.microsoft.com/en-u ... .aspx
,
http://msdn.microsoft.com/en-u ... .aspx
/, 您可以尝试附加的。 但是,我不确定这些是瘾的属性。

另一个变体 - 查找实际控制元素
ScrollBar

在视觉树上并将它们绑在财产上
Position

他们每个人。

要回复问题请先登录注册