[英]Autoscroll ListView in WPF using MVVM
I try to have a list that automatically scrolls to the end when adding a new line. 我尝试有一个添加新行时自动滚动到末尾的列表。
Here is a simple MVVM example in which I want to integrate : 这是我要集成的简单MVVM示例:
There is a button that adds lines to the list when clicked. 单击时有一个按钮可将行添加到列表中。
Model code : 型号代码:
public class Student
{
public string Lastname {get; set;}
public string Firstname {get; set;}
public Student(string lastname, string firstname) {
this.Lastname = lastname;
this.Firstname = firstname;
}
}
public class StudentsModel: ObservableCollection<Student>
{
private static object _threadLock = new Object();
private static StudentsModel current = null;
public static StudentsModel Current {
get {
lock (_threadLock)
if (current == null)
current = new StudentsModel();
return current;
}
}
private StudentsModel() {
for (int i = 1; i <= 50; i++)
{
Student aStudent = new Student("Student " + i.ToString(), "Student " + i.ToString());
Add(aStudent);
}
}
public void AddAStudent(String lastname, string firstname) {
Student aNewStudent = new Student(lastname, firstname);
Add(aNewStudent);
}
}
ViewModel code : ViewModel代码:
public class MainViewModel : ViewModelBase
{
public StudentsModel Students { get; set; }
public MainViewModel()
{
Students = StudentsModel.Current;
}
private ICommand _AddStudent;
public ICommand AddStudent
{
get
{
if (_AddStudent == null)
{
_AddStudent = new DelegateCommand(delegate()
{
Students.AddAStudent("New Student lastname", "New Student firstname");
});
}
return _AddStudent;
}
}
View code : 查看代码:
<Window x:Class="demo.Views.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:demo.Commands">
<Grid>
<ListView Grid.Row="2" BorderBrush="White" ItemsSource="{Binding Path=Students}"
HorizontalAlignment="Stretch">
<ListView.View>
<GridView>
<GridViewColumn Header="Lastname" DisplayMemberBinding="{Binding Path=Lastname}" />
<GridViewColumn Header="Firstname" DisplayMemberBinding="{Binding Path=Firstname}" />
</GridView>
</ListView.View>
</ListView >
<Button Content="Add" Command="{Binding AddStudent}" Margin="601.94,36.866,96.567,419.403" />
</Grid>
Thank you 谢谢
I wrote a simple AttachedProperty that I use to auto-scroll to the bottom when a bound ObservableCollection changes: 我写了一个简单的AttachedProperty,当绑定的ObservableCollection更改时,它用于自动滚动到底部:
public class AutoScroller : Behavior<ScrollViewer>
{
public object AutoScrollTrigger
{
get { return (object)GetValue( AutoScrollTriggerProperty ); }
set { SetValue( AutoScrollTriggerProperty, value ); }
}
public static readonly DependencyProperty AutoScrollTriggerProperty =
DependencyProperty.Register(
"AutoScrollTrigger",
typeof( object ),
typeof( AutoScroller ),
new PropertyMetadata( null, ASTPropertyChanged ) );
private static void ASTPropertyChanged( DependencyObject d, DependencyPropertyChangedEventArgs args )
{
var ts = d as AutoScroller;
if( ts == null )
return;
// must be attached to a ScrollViewer
var sv = ts.AssociatedObject as ScrollViewer;
// check if we are attached to an ObservableCollection, in which case we
// will subscribe to CollectionChanged so that we scroll when stuff is added/removed
var ncol = args.NewValue as INotifyCollectionChanged;
// new event handler
if( ncol != null )
ncol.CollectionChanged += ts.OnCollectionChanged;
// remove old eventhandler
var ocol = args.OldValue as INotifyCollectionChanged;
if( ocol != null )
ocol.CollectionChanged -= ts.OnCollectionChanged;
// also scroll to bottom when the bound object itself changes
if( sv != null && ts.AutoScroll )
sv.ScrollToBottom();
}
private void OnCollectionChanged(object sender, EventArgs args)
{
App.Current.Dispatcher.Invoke(delegate {
(this.AssociatedObject as ScrollViewer).ScrollToBottom();
});
}
}
Note: I use Rx to subscribe to the CollectionChanged event, but that can be done with normal .NET event handling plus Dispatcher.Invoke to get the .ScrollToBottom() call on the UI thread. 注意:我使用Rx来订阅CollectionChanged事件,但是可以通过常规.NET事件处理加上Dispatcher.Invoke来完成,以在UI线程上获取.ScrollToBottom()调用。
Also Note: This attached property is in a Behavior class called TouchScroller, which does other stuff too, but it can be simplified to just a simple attached property. 另请注意:该附加属性在名为TouchScroller的Behavior类中,该类也可以进行其他操作,但是可以将其简化为简单的附加属性。
EDIT: 编辑:
To use it, in the xaml you would simply bind the property from the viewmodel: 要使用它,只需在xaml中绑定视图模型中的属性即可:
<ScrollViewer ...>
<i:Interaction.Behaviors>
<util:TouchScroller AutoScrollTrigger="{Binding Students}" />
</i:Interaction.Behaviors>
...
</ScrollViewer>
EDIT2: EDIT2:
I edited the code to contain a full Behavior. 我编辑了代码以包含完整的行为。 I use a Behavior instead of simpley a static class with an attached behavior because it gives access to
.AssociatedObject
, which is the ScrollViewer that you need to call .ScrollToBottom()
on. 我使用Behavior代替简单的带有附加行为的静态类,因为它可以访问
.AssociatedObject
,这是您需要调用.ScrollToBottom()
的ScrollViewer。 Without using a behavior, you would have to keep track of these objects manually. 如果不使用任何行为,则必须手动跟踪这些对象。 I also removed the Rx subscription and added simple event handlers and Dispatcher.Invoke.
我还删除了Rx订阅,并添加了简单的事件处理程序和Dispatcher.Invoke。
This is a stripped down and modified version of what I use, but I haven't tested this version. 这是我使用的精简版和修改版,但我尚未测试此版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.