设置垂直滚动条位置取决于列表中选定的项目
在我的小应用程序中,有一个按钮,一旦单击,列表视图中的第五个项目就会被选中。
问题是它超出了列表视图范围,我希望垂直滚动条向下滑动到 ,以便我可以看到所选项目。
我怎样才能做到这一点?
XAML:
<StackPanel>
<Button Click="Button_Click">Find number 5</Button>
<ListView x:Name="lst" ScrollViewer.VerticalScrollBarVisibility="Visible" Height="50">
<ListView.Items>
<ListViewItem>1</ListViewItem>
<ListViewItem>2</ListViewItem>
<ListViewItem>3</ListViewItem>
<ListViewItem>4</ListViewItem>
<ListViewItem>5</ListViewItem>
<ListViewItem>6</ListViewItem>
<ListViewItem>7</ListViewItem>
</ListView.Items>
</ListView>
</StackPanel>
C# 代码:
public partial class Window2 : Window
{
public Window2()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
((ListViewItem)lst.Items[4]).IsSelected = true;
}
}
In my little app there's a button that once getting clicked, the fifth item in the listview getting selected.
the problem is that its out of the listview scope and I would like the vertical scroll bar to slide down to the so I can see the selected item.
How can I do that ?
XAML:
<StackPanel>
<Button Click="Button_Click">Find number 5</Button>
<ListView x:Name="lst" ScrollViewer.VerticalScrollBarVisibility="Visible" Height="50">
<ListView.Items>
<ListViewItem>1</ListViewItem>
<ListViewItem>2</ListViewItem>
<ListViewItem>3</ListViewItem>
<ListViewItem>4</ListViewItem>
<ListViewItem>5</ListViewItem>
<ListViewItem>6</ListViewItem>
<ListViewItem>7</ListViewItem>
</ListView.Items>
</ListView>
</StackPanel>
C# code:
public partial class Window2 : Window
{
public Window2()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
((ListViewItem)lst.Items[4]).IsSelected = true;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 ListView.ScrollIntoView 来实现你想要的:
Use ListView.ScrollIntoView to achieve what you want: