选择并聚焦列表框项 [英] Select and focus a listbox-item

查看:77
本文介绍了选择并聚焦列表框项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF ListBox,其ItemsSource绑定到MyObjects(产品)的ViewModels列表。这很好用,lisbox将填充数据库中的产品。不幸的是,DB中有超过4000个条目。当用户导航到列表框时,应选择并聚焦特定产品条目,就像用户使用鼠标单击特定条目一样。在WPF中解决这个问题的一种简单方法是什么?

将所选项目与XAML绑定如下:

I have a WPF ListBox with the ItemsSource bound to a ViewModels list of MyObjects (products). This works fine, the lisbox will be filled with products from Database. Unfortunatly there are over 4.000 entries in the DB. When the user navigates to the listbox, a specific product entry should be selected and focused, just like when the user clicks a specific entry with the mouse. Is the a simple way in WPF to solve this?
Binding the selected item with XAML like this works:

<ListBox SelectedItem="{Binding SelectedArtikel, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />



但所选项目位于列表深处。所选项目被标记为已选中但不在视线范围内,您必须手动向下滚动。



问候



我尝试过:



我试过:


but the selected item is somewhere deep in the list. The selected item is marked as selected but not in sight, you have to scroll down manually.

Regards

What I have tried:

I tried:

lbxArtikel.UpdateLayout();
var lbxItem = (ListBoxItem)lbxArtikel
	.ItemContainerGenerator
	.ContainerFromItem(lbxArtikel.SelectedItem);
lbxItem.Focus();

但是这只选择了第一个条目。

But this selected only the first entry.

推荐答案

lbxArtikel.ScrollIntoView(lbxArtikel.SelectedItem)



thx to UGLeander!


thx to U.G.Leander!


这篇关于选择并聚焦列表框项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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