我在以下问题上找到了“ Nick Miller”的示例代码: mvvm how to make a list view auto scroll to a new Item in a list view
这部分回答了我的问题,因为滚动工作非常紧密,但是我需要用户可以停止自动滚动。
为此,我添加了一个复选框,并在事件中将变量listbox.autoscroll设置为true或false,但这无济于事,尽管代码将更改属性。
我没有更改LoggingListBox类上的Nick Miller代码。
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Test_Listbox_Autoscroll"
xmlns:tools="clr-namespace:Test_Listbox_Autoscroll"
mc:Ignorable="d"
Title="MainWindow" Width="800"
SizeToContent =" WidthAndHeight">
<Grid>
<StackPanel >
<tools:LoggingListBox x:Name="Listbox1"
Margin=" 10"
Background="Bisque"
Height="180"
ItemsSource="{Binding LST_ListBox1_Collection}"
SelectedItem="{Binding LST_ListBox1_SelectedItem}"/>
<CheckBox x:Name="ChkAutoScroll"
Margin="10"
Content="Auto Scroll"
Click="ChkAutoScroll_Click"/>
</StackPanel>
</Grid>
</Window>
Private Sub ChkAutoScroll_Click(sender As Object, e As RoutedEventArgs)
If (ChkAutoScroll.IsChecked = True) Then
Listbox1.AutoScroll = True
Else
Listbox1.AutoScroll = False
End If
End Sub
没有错误消息,但是尽管AutoScroll值已更改,滚动仍处于活动状态。