I have a ContentTemplateSelector which is successfully changing as intended. It is in a listbox, which contains a bunch of textblocks, then when you select an item in the listbox, that item changes it's template to be have a textbox (so it can be edited).
The problem I have is that the textbox doesn't get focus. Which is slightly annoying.
So I have been trying to implement an Attached Property for the Listbox, which, when set, will attach an eventhandler to the SelectionChanged Event. From there, I am hoping to drill down, find the textbox and set its focus.
However, I have reached a snag, where it seems that the TemplateSelector has not yet selected the appropriate template (the one with the textbox) yet. In fact, the Template is NULL. Which leads me to believe it has already removed the TextBlock template and will soon be selecting the correct template, but only after this whole SelectionChanged event has completed.
My question is either, do you know of a way to force this DataTemplate to tell it's ContentTemplateSelector to choose the appropriate template, or perhaps, a different idea about how I can select this textbox inside the Template when the ListboxItem gets focus?
Here is how I get the ContentPresenter (From the ListboxItem that is selected):
ContentPresenter cp = FindVisualChild<ContentPresenter>(lbi);
EDIT: I've added cp.ApplyTemplate() after this, but that doesn't change anything.
cp.ContentTemplate is null at this point though.
So I figured it out myself and wanted to post my answer in case anyone else has a similar problem.
First off, in my TemplateSelector, added a property for the last selected template, which I can get to in code (called SelectedTemplate).
Then did the following in the event of the listbox selection changed event: