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

  • Home
  • SEARCH
  • 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 6027043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:31:59+00:00 2026-05-23T04:31:59+00:00

Situation: I have a wrapper panel UserControl like this (namespaces and visual details removed

  • 0

Situation:

I have a “wrapper panel” UserControl like this (namespaces and visual details removed for brevity):

<UserControl ...>
<Grid x:Name="LayoutRoot" Background="White">
    <ContentPresenter x:Name="integratedPanelContent" Margin="5" /> 
</Grid>
</UserControl>

Then in the Code-behind I have registered a dependency property

public FrameworkElement PanelContent
{
    get { return (FrameworkElement)GetValue(PanelContentProperty); }
    set { SetValue(PanelContentProperty, value); }
}
public static readonly DependencyProperty PanelContentProperty =
    DependencyProperty.Register("PanelContent", typeof(FrameworkElement), typeof(MyWrapperPanel),
      new PropertyMetadata(null, OnPanelContentChanged));

private static void OnPanelContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    ((MyWrapperPanel)d).OnSetContentChanged(e);
}

protected virtual void OnSetContentChanged(DependencyPropertyChangedEventArgs e)
{
    if (PanelContent != null)
        integratedPanelContent.Content = PanelContent;
}

Now I can wrap any content into my control:

<my:MyWrapperPanel x:Name="myWrap">
    <my:MyWrapperPanel.PanelContent>
        <TextBlock x:Name="tbxNothing" Text="Nothing" />
    </my:MyWrapperPanel.PanelContent>
</my:MyWrapperPanel>

Description of the problem:

Whenever I try to use the reference tbxNothing in codebehind, the system throws NullReferenceException because tbxNothing, although as a reference exists, does not point to the TextBlock defined in XAML, but is null.

Possible (but inconvenient) workaround:

There is a workaround where I remove x:Name from the TextBlock, and then I explicitely define private TextBlock called tbxNothing. Then in the OnNavigatedTo event handler I assign the value the following way:

tbxNothing = myWrap.PanelContent as TextBlock;

This works but is not a right way to do it, because if a content is a stackpanel that contains wanted controls, I’d have to traverse the tree to find what I need, which is extremely inconvenient.

Question:

Why is the textblock no longer visible when wrapped in a User control (the way described), and how to get it by its x:Name in code-behind?

  • 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-23T04:32:00+00:00Added an answer on May 23, 2026 at 4:32 am

    The problem is your panel content is falling between two stools. On the one hand the content with the name “tbxNothing” is create in the namescope of the main page. However its not added to the object tree at that point. On the other hand the MyWrapperPanel being a UserControl has its own namescope and its into the object tree below this that the item with then name “tbxNothing” is added. FindName on the main page won’t find anything inside the MyWrapperPanel because it has its own namescope and FindName on the MyWrapperPanel won’t find “tbxNothing” because it doesn’t exist in its namescope (being actually created in the main page).

    The answer is don’t use a UserControl as a basis for MyWrapperPanel. Instead create a Silverlight Template Control. Modify the base class it inherits from to ContentControl and tweak its default template to include a ContentPresenter. Should look something like this:-

    public class MyWrapperPanel : ContentControl
    {
        public MyWrapperPanel ()
        {
            this.DefaultStyleKey = typeof(MyWrapperPanel );
        }
    }
    

    then in themes/generic.xaml the style can look like this:-

    <Style TargetType="local:MyWrapperPanel">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:MyWrapperPanel">
                    <Grid>
                        <ContentPresenter />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    Your main page xaml would look like:-

    <my:MyWrapperPanel x:Name="myWrap">
        <TextBlock x:Name="tbxNothing" Text="Nothing" />
    </my:MyWrapperPanel>
    

    Note that deriving from ContentControl gives you a Content property which the ContentPresenter auto-magically wires to.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a pretty odd situation I have here. I have used a piece
I have such situation: There is 8 div-blocks with ids like 'rateN_wrapper' where is
I'm wondering if this is possible. I have html like so: <p> <font face=Georgia>
Situation: I have a simple XML document that contains image information. I need to
I have following situation: I have loged user, standard authentication with DB table $authAdapter
I have the following situation: I have a certain function that runs a loop
The situation: I have a pieceofcrapuous laptop. One of the things that make it
My situation: I have several components, which sometimes have changes to them, and are
My Situation I have a N rectangles The rectangles all have the same shape
Here's the situation: I have a label's text set, immediately followed by a response.redirect()

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.