我有一个非常简单的问题,但我无法弄清楚如何使用MVVM破解它.
我有一个ListBox必然的ObservableCollection<string>.
我运行一个过程,将一大堆项目添加到集合中,因此它们显示在ListBox.
问题是,当项目被添加到列表框...滚动条只是增长,但我似乎无法弄清楚如何为ScrollIntoView添加到集合中的每个项目.
此示例代码完美地说明了该问题.
XAML
<Window x:Class="Stack.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:Stack"
Title="MainWindow"
Height="350"
Width="525">
<Window.DataContext>
<vm:MainWindowViewModel />
</Window.DataContext>
<StackPanel>
<ListBox Margin="10" Height="150"
ItemsSource="{Binding Path=MyValue}" />
<Button Margin="10"
Height="25"
Content="Generate"
Command="{Binding Path=CommandName}" />
</StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)
查看模型
namespace Stack
{
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
/// <summary>
/// TODO: Update summary.
/// </summary>
public class MainWindowViewModel : INotifyPropertyChanged
{
private readonly BackgroundWorker _worker;
private ICommand _commandName;
private …Run Code Online (Sandbox Code Playgroud)