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 7665477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:34:40+00:00 2026-05-31T14:34:40+00:00

How to access the tbRegistrationBtn.text property from code behind from a custom made style?

  • 0

How to access the tbRegistrationBtn.text property from code behind from a custom made style?

My button is being created dynamically from codebehind and gets added to the parent control (stackpanel):
The button gets created when i press a other button on my screen.

Codebehind:

                Button newBtn = new Button();
                newBtn.Width = 160;
                newBtn.Height = 46;
                newBtn.Style = this.FindResource("ButtonStyleRegistration") as Style;
                spHorizontal.Children.Add(newBtn);

Xaml:

        <Style x:Key="ButtonStyleRegistration" TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid x:Name="registrationButton">
                        <Rectangle Fill="#FF89959A" Height="Auto" RadiusY="15" RadiusX="15" Stroke="White" Width="Auto"/>
                        <TextBlock x:Name="tbRegistrationBtn" TextWrapping="Wrap" Text="" HorizontalAlignment="Center" Margin="7.5,14.973,0,16.84" d:LayoutOverrides="Height"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsFocused" Value="True"/>
                        <Trigger Property="IsDefaulted" Value="True"/>
                        <Trigger Property="IsMouseOver" Value="True"/>
                        <Trigger Property="IsPressed" Value="True"/>
                        <Trigger Property="IsEnabled" Value="False"/>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="FontSize" Value="10.667"/>
    </Style> 

Any attempt to retrieve the textblock results in a null error.
Attempt:

            Style style = this.FindResource("ButtonStyleRegistration") as Style;
            newBtn.Style = style;
            TextBlock tb = (TextBlock)style.Resources.FindName("tbRegistrationBtn");
            tb.Text = "test";

Best regards.

  • 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-31T14:34:41+00:00Added an answer on May 31, 2026 at 2:34 pm

    You can use VisualTreeHelper to navigate though visual tree of your button. Use this helper function:

    public static T FindVisualChild<T>(DependencyObject obj, string name)
        where T : DependencyObject
    {
        var count = VisualTreeHelper.GetChildrenCount(obj);
        for(int i = 0; i < count; i++)
        {
            var child = VisualTreeHelper.GetChild(obj, i);
    
            if(child != null)
            {
                var res = child as T;
                if(res != null && (string)res.GetValue(FrameworkElement.NameProperty) == name)
                {
                    return res;
                }
                res = FindVisualChild<T>(child, name);
                if(res != null) return res;
            }
        }
    
        return null;
    }
    

    Also you need to force your button to build it’s visual tree based on template (because it is delayed by default):

    newBtn.ApplyTemplate();
    

    And finally set your TextBlock text:

    var tb = FindVisualChild<TextBlock>(newBtn, "tbRegistrationBtn");
    tb.Text = "Registration";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

MS Access has a button to generate sql code for finding duplicated rows. I
MS Access appears to support nulls in code, but I can't for the life
ms-access is connecting to a local mysql database the following code returns an error:
in access i am doing this: insert into accounts_changes (select * from accounts where
cannot access asp.net server side control from jquery Label <label id=lblClanName runat=server>Something here</label> Jquery
In Access 2003, there are ways of running code when a form or database
I access cassandra from the ruby gem. I want to retrive data from cassandra
In Access is it possible to change the caption of a button based upon
Root access is possible. Basically I need a way to read content: //settings/system/alarm_alert from
//ACCESS SELECT DISTINCT products.imageUrl FROM products WHERE ((products.pcprod_ParentPrd=5573) AND (products.pcprod_Relationship LIKE '*441*')); //ASP SELECT

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.