How can I access the ListViewItems of a WPF ListView?

前端 未结 6 920
天命终不由人
天命终不由人 2020-12-09 10:33

Within an event, I\'d like to put the focus on a specific TextBox within the ListViewItem\'s template. The XAML looks like this:



        
6条回答
  •  旧巷少年郎
    2020-12-09 11:21

    To understand why ContainerFromItem didn't work for me, here some background. The event handler where I needed this functionality looks like this:

    var item = new SomeListItem();
    SomeList.Add(item);
    ListViewItem = SomeList.ItemContainerGenerator.ContainerFromItem(item); // returns null
    

    After the Add() the ItemContainerGenerator doesn't immediately create the container, because the CollectionChanged 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 a StatusChanged 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.

提交回复
热议问题