如何访问 WPF ListView 的 ListViewItems?
在一个事件中,我想将焦点放在 ListViewItem 模板中的特定 TextBox 上。 XAML 看起来像这样:
<ListView x:Name="myList" ItemsSource="{Binding SomeList}">
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<!-- Focus this! -->
<TextBox x:Name="myBox"/>
我在后面的代码中尝试了以下操作:
(myList.FindName("myBox") as TextBox).Focus();
但我似乎误解了 FindName()
文档,因为它返回 null
。
此外,ListView.Items
也没有帮助,因为(当然)它包含我绑定的业务对象,但没有 ListViewItems。
myList.ItemContainerGenerator.ContainerFromItem(item)
也不会,它也返回 null。
Within an event, I'd like to put the focus on a specific TextBox within the ListViewItem's template. The XAML looks like this:
<ListView x:Name="myList" ItemsSource="{Binding SomeList}">
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<!-- Focus this! -->
<TextBox x:Name="myBox"/>
I've tried the following in the code behind:
(myList.FindName("myBox") as TextBox).Focus();
but I seem to have misunderstood the FindName()
docs, because it returns null
.
Also the ListView.Items
doesn't help, because that (of course) contains my bound business objects and no ListViewItems.
Neither does myList.ItemContainerGenerator.ContainerFromItem(item)
, which also returns null.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以向上遍历 ViewTree 以查找与命中测试触发的单元格对应的项“ListViewItem”记录集。
同样,您可以从父视图获取列标题来比较和匹配单元格的列。 您可能希望将单元格名称绑定到列标题名称,作为比较器委托/过滤器的键。
例如:HitResult 在 TextBlock 上显示为绿色。 您希望获取“ListViewItem”的句柄。
You can traverse up the ViewTree to find the item 'ListViewItem' record set that corresponds to the cell triggered from hit test.
Similarly, you can get the column headers from the parent view to compare and match the cell's column. You may want to bind the cell name to the column header name as your key for your comparator delegate/filter.
For example: HitResult is on TextBlock shown in green. You wish to obtain the handle to the 'ListViewItem'.
要了解为什么
ContainerFromItem
对我不起作用,这里有一些背景知识。 我需要此功能的事件处理程序如下所示:在
Add()
之后,ItemContainerGenerator
不会立即创建容器,因为CollectionChanged
> 事件可以在非 UI 线程上处理。 相反,它启动异步调用并等待 UI 线程回调并执行实际的 ListViewItem 控件生成。为了在发生这种情况时收到通知,
ItemContainerGenerator
公开一个StatusChanged
事件,该事件在生成所有容器后触发。现在我必须监听这个事件并决定控件当前是否想要设置焦点。
To understand why
ContainerFromItem
didn't work for me, here some background. The event handler where I needed this functionality looks like this:After the
Add()
theItemContainerGenerator
doesn't immediately create the container, because theCollectionChanged
event could be handled on a non-UI-thread. Instead it starts an asynchronous call and waits for the UI thread to callback and execute the actual ListViewItem control generation.To be notified when this happens, the
ItemContainerGenerator
exposes aStatusChanged
event which is fired after all Containers are generated.Now I have to listen to this event and decide whether the control currently want's to set focus or not.
我们在 WPF 的新数据网格中使用了类似的技术:
该技术应该非常适合您,只需在生成后传递您的 listviewitem 即可。
We use a similar technique with WPF's new datagrid:
The technique should be fairly applicable for you, just pass your listviewitem once it's generated.
正如其他人所指出的,通过在 ListView 上调用 FindName 无法找到 myBox TextBox。 但是,您可以获取当前选定的ListViewItem,并使用VisualTreeHelper 类从ListViewItem 获取TextBox。 这样做看起来像这样:
As others have noted, The myBox TextBox can not be found by calling FindName on the ListView. However, you can get the ListViewItem that is currently selected, and use the VisualTreeHelper class to get the TextBox from the ListViewItem. To do so looks something like this:
或者可以简单地通过以下方式完成
Or it can be simply done by
我注意到问题标题与问题内容没有直接关系,接受的答案也没有回答它。 我已经能够通过使用以下方法“访问 WPF ListView 的 ListViewItems”:
我不确定递归有多忙,但在我的情况下它似乎工作得很好。 不,我之前没有在递归上下文中使用过yield return。
I noticed that the question title does not directly relate to the content of the question, and neither does the accepted answer answer it. I have been able to "access the ListViewItems of a WPF ListView" by using this:
I'm not sure how hectic the recursion gets, but it seemed to work fine in my case. And no, I have not used
yield return
in a recursive context before.