WPF的ItemsControl - 如何知道什么时候项目完成加载,以便我可以集中第一个? [英] WPF ItemsControl - how to know when the items finished loading, so that I can focus the first one?
问题描述
我想将键盘焦点移到第一个项目在一个ItemsControl中,但是当我在View的代码隐藏处理SearchFinished的时候,这些项目还没有渲染(集合已经被填充了,但是wpf的渲染是异步的,并没有发生),所以没有什么可以关注的(Focus()需要已经构建了项目的可视化树)。
我想做(myItemsControl.ItemContainerGenerator.ContainerFromIndex(0)作为UIElement).Focus();但作为第零项还没有加载,ContainerFromIndex(0 )返回null。
我尝试用Dispatcher.BeginInvoke ...延迟它的优先级低,但这是依赖于确切的时间,通常不起作用。 p>
如何等待ItemsControl中的第一个项目被加载?
您可以使用 ItemContainerGenerator.StatusChanged
事件,然后检查它的 Status
属性。如果 Status == GeneratorStatus.ContainersGenerated
,那么您可以安全地获取第一个容器。
I have an ItemsControl in my View, that is bound to an ObservableCollection from ViewModel. The collection is filled, and afterwards an event from VM to view is raised (think search results and SearchFinished event).
I would like to move keyboard focus to the first item in an ItemsControl, but when I do it in View's code-behind when handling SearchFinished, the items are not yet rendered (the collection is filled already, but wpf's rendering is asynchronous and didn't happen yet), so there is nothing to focus (Focus() needs to have the items' visual tree already constructed).
I wanted to do (myItemsControl.ItemContainerGenerator.ContainerFromIndex(0) as UIElement).Focus();, but as the 0th item is not yet loaded, ContainerFromIndex(0) returns null.
I tried delaying it with Dispatcher.BeginInvoke... with low priority, but that is dependent on exact timing and usually doesn't work.
How can I wait until the first item in ItemsControl is Loaded?
You can use the ItemContainerGenerator.StatusChanged
event, and then check its Status
property. If the Status == GeneratorStatus.ContainersGenerated
, then you can safely get the first container.
这篇关于WPF的ItemsControl - 如何知道什么时候项目完成加载,以便我可以集中第一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!