Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 608521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:28:12+00:00 2026-05-13T17:28:12+00:00

I have seen solutions to a very similar issue, yet it doesn’t translate to

  • 0

I have seen solutions to a very similar issue, yet it doesn’t translate to mine. (Namely, this article: http://blogs.msdn.com/wpfsdk/archive/2007/04/16/how-do-i-programmatically-interact-with-template-generated-elements-part-ii.aspx)

My ItemsControl is bound to an observable collection, which can have items dynamically added to it.

When I add an item to the observable collection, the templated item renders properly in my itemscontrol, but I can’t figure out how to access it. My my observable colleciton changed code, I am trying to access information about. I am using a custom DataTemplateSelector to return one of 3 different dataTemplates, based on the item’s data in the collection.

Here is an outline of my ItemsControl XAML:

<ItemsControl Name="myItemsControl" ItemTemplateSelector="{StaticResource myTempSelector}">
    <ItemsControl.Template>
        <ControlTemplate TargetType="ItemsControl">
            <ItemsPresenter/>
        </ControlTemplate>
    </ItemsControl.Template>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel></StackPanel>   
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

    </ItemsControl>

The solutions I’ve seen suggest using ItemContainerGenerator.ContainerFromItem(xxx)

In this examples, they are always looking for information about a ListBox or ComboBox (which inherit from ContentControl). However, when I call (in my code-behind) myItemsControl.ItemContainerGenerator.ContainerFromItem(xxx), I receive a ContentPresenter, rather than the ContentControl I expect.

Then, when I try to access the ContentTemplate of this ContentPresenter, I get a null object exception.

I have a hunch that the rest of my troubles descend from there.

All I want to do is find a textbox from the datatemplate in my newly created control, and give it focus.

Help! 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T17:28:13+00:00Added an answer on May 13, 2026 at 5:28 pm

    You need to get a handle to the DataTemplate itself, and use its FindName method, referencing the parent control of your item.

    For example:

    var item = myItemsControl.ItemContainerGenerator.ContainerFromItem(xxx);
    var template = this.Resources["MyItemTemplate"] as DataTemplate;
    var ctl = template.FindName("textBox1", item) as FrameworkElement;
    

    So this finds a control called “textBox1” inside the item.

    If you’re not using a named DataTemplate (ie one with x:Key=”MyItemTemplate”) and instead using DataType=”…” to define a DataTemplate to be used for specific types, the method by which you find the template changes slightly:

    var actionKey = new DataTemplateKey(typeof(MyCustomClass));
    var actionTemplate = Resources[actionKey] as DataTemplate;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My issue can be seen here: http://jasonnarciso.com/dev/cmwd/concrete-solutions/ Basically I have a very simple jquery
I have seen solutions like this: kernel dp_square (const float *a, float *result) {
I have seen n-tiers solutions on the web, they all use DLLs for each
i have seen some hand rolled solutions but does jquery out of the box
I have seen lots of questions and solutions on File upload using Selenium WebDriver
I have tried several of the solutions I have seen out here, but am
I have seen the solution to this a while back, but can't seem to
Have seen some similar questions: What is the difference between a JavaBean and a
I have seen this question asked in a couple of different ways on SO
Disclaimer: I have already seen the following questions and their solutions did not apply

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.