pkp9774

I've got an ItemsControl which is DataBound to an ObservableCollection<> source. I can add an item to the collection fine and the new DataTemplate'd item shows up fine.

How can I get access to the new control added via the DataTemplate so that I can set focus on this new element



Re: Windows Presentation Foundation (WPF) VisualChildren of ItemsControl

Bragi

You can use the ItemContainerGenerator property of the ItemsControl to get the container of the item you just added by calling one of it's functions (ContainerFromIndex, ContainerFromItem).





Re: Windows Presentation Foundation (WPF) VisualChildren of ItemsControl

pkp9774

Thanks for the reply, this looks in the direction I'm needing to go but I've run into another snag.

I'm using ItemContainerGenerator.ContainerFromItem( [Item I added to the collection]) which returns a ContentPresenter. When I try to access the Content property of the ContentPresenter its the item that I added, not the UIElement.

How can I use this ContentPresenter to access the UIElement that represents the Item I've added





Re: Windows Presentation Foundation (WPF) VisualChildren of ItemsControl

Bragi

Tha'ts a bit more difficult. You could use the VisualTreeHelper class to find the children of the ContentPresenter. One of them is the UIElement. You could also try something like:

Code Block

TextBox iText = aItem.ContentTemplate.FindName("TxtContent", aItem) as TextBox;

Where TxtContent is the name of the UIElement you are trying to find and aItem is the ContentPresenter.





Re: Windows Presentation Foundation (WPF) VisualChildren of ItemsControl

pkp9774

This worked for me, thanks! I've now realized that I cannot get a reference to the element the moment I add the item to the collection, apparently its not created until the PropertyChanged event is handled (makes sense).

Is there an event I can tie into that tells me when the element has been created so that I may set focus to it





Re: Windows Presentation Foundation (WPF) VisualChildren of ItemsControl

pkp9774

It looks like I would want to use the ItemContainerGenerator.ItemsChanged event which seems to tell me what I need to know (that an item has been added), however it appears that it is still prior to the point in time where I can do something about it (before the UIElement has actually been created).

This seems to be a recurring theme here where I can be notified of something but before I can actually do something with the result of the event (what the event was telling me was happening).

Is there a standard solution here I have seen a number of posts for other issues that seem to solve it using timers but that seems a clumsy way to deal with the problem.

Anyone have any ideas






Re: Windows Presentation Foundation (WPF) VisualChildren of ItemsControl

Josh Smith

You could try using a call to Dispatcher.BeginInvoke() passing in 'Background' or 'AppIdle' as the priority. The callback would assumably be invoked after the element creation callstack has finished.






Re: Windows Presentation Foundation (WPF) VisualChildren of ItemsControl

pkp9774

Yeah, that's what I settled on, it at-least looks cleaner than doing the timer object setup.

I take it there isn't a standard mechanism to deal with this problem

Thank you all for your help!




Re: Windows Presentation Foundation (WPF) VisualChildren of ItemsControl

Bragi

Try calling 'UpdateLayout' on the ItemsControl before you call FindName, this should update the control and all visuals should be drawn again.