设置项目Focus在WPF的ListView [英] Set Item Focus in ListView WPF

查看:515
本文介绍了设置项目Focus在WPF的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法来完成从的WinForms在WPF此功能?

is there any way to accomplish this functionality from WinForms in WPF?

ListView.FocusedItem = ListView.Items[itemToFocusIndex]

我想手动设置的焦点在WPF ListView项(不选择)。从 System.Windows.Controls的的。
谢谢你。

I'm trying to manually set focus (not select) on item in WPF ListView. From System.Windows.Controls. Thanks.

推荐答案

有两种类型的焦点在WPF中 - 键盘焦点和逻辑焦点。 此链接可以给你的焦点在WPF的更多信息。

There are two types of focus in WPF - Keyboard Focus and Logical Focus. This link can give you more information about focus in WPF.

您可以做到这一点:

ListViewItem item = myListView.ItemContainerGenerator.ContainerFromIndex(index) as ListViewItem;
item.Focus();

它也可以调用

Keyboard.Focus(item); 

如果你也想滚动的ListView 的项目的位置,加上这一句:

If you also want to scroll the ListView to the item's position, add this:

myListView.ScrollIntoView(item);

重要提示:对于这个工作,你需要设置 VirtualizingStackPanel.IsVirtualizing =FALSE的ListView ,这可能导致其执行速度较慢。需要该附加属性的原因是,当的ListView 是虚拟化(这是默认值), ListViewItems 不为没有在屏幕上显示的项目,这将导致创建 ContainerFromIndex()返回

IMPORTANT NOTE: For this to work, you will need to set VirtualizingStackPanel.IsVirtualizing="False" on your ListView, which may cause it to perform slower. The reason this attached property is required is that when the ListView is virtualized (which it is by default), the ListViewItems aren't created for items that aren't displayed on the screen, which will cause ContainerFromIndex() to return null.

这篇关于设置项目Focus在WPF的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆