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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:40:40+00:00 2026-05-18T02:40:40+00:00

I have the following ControlTemplate : <ControlTemplate> <Grid VerticalAlignment=Stretch HorizontalAlignment=Left Width=400> <Grid.ColumnDefinitions> <ColumnDefinition Width=18

  • 0

I have the following ControlTemplate:

<ControlTemplate>
    <Grid VerticalAlignment="Stretch" HorizontalAlignment="Left" Width="400">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="18" />
            <ColumnDefinition Width="20*" />
            <ColumnDefinition Width="20*" />
            <ColumnDefinition Width="20*" />
            <ColumnDefinition Width="45" />
        </Grid.ColumnDefinitions>
        <TextBox Grid.Column="1" Template="{StaticResource watermark}" HorizontalAlignment="Stretch" Margin="4,0,0,4" Tag="Number" />
        <TextBox Grid.Column="2" Template="{StaticResource watermark}" HorizontalAlignment="Stretch" Margin="4,0,0,4" Tag="Login" />
        <TextBox Grid.Column="3" Template="{StaticResource watermark}" HorizontalAlignment="Stretch" Margin="4,0,0,4" Tag="Password" />
        <Button Grid.Column="4" HorizontalAlignment="Stretch" Content="Add" Margin="4,0,0,4" Click="AddUser_Click"/>
    </Grid>
</ControlTemplate>

How should I write AddUser_Click to get access to textboxes Text properties?

upd: just to make it clear. I know how to connect Click event handler here. The question is how to read contents of textboxes in it since I cant give them name because they are in a template.

  • 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-18T02:40:41+00:00Added an answer on May 18, 2026 at 2:40 am

    If you have access to the templatedparent (SelectedItem, FindVisualParent etc.) you can do this if you apply Names to the TextBoxes. Example if ControlTemplate is for ComboBoxItem.

    private void AddUser_Click(object sender, RoutedEventArgs e)
    {
        ComboBoxItem comboBoxItem = GetVisualParent<ComboBoxItem>(button);
        TextBox textBox = comboBoxItem.Template.FindName("numberTextBox", comboBoxItem) as TextBox;
        //...
    }
    

    Another way to get the TextBoxes within the ControlTemplate would be to use the Visual Tree. Something like this

    private void AddUser_Click(object sender, RoutedEventArgs e)
    {
        Button button = sender as Button;
        Grid parentGrid = GetVisualParent<Grid>(button);
        List<TextBox> textBoxes = GetVisualChildCollection<TextBox>(parentGrid);
        foreach (TextBox textBox in textBoxes)
        {
            if (textBox.Tag == "Number")
            {
                // Do something..
            }
            else if (textBox.Tag == "Login")
            {
                // Do something..
            }
            else if (textBox.Tag == "Password")
            {
                // Do something..
            }
        }
    }
    

    And an implementation of GetVisualParent and GetVisualChildCollection

    public static T GetVisualParent<T>(object childObject) where T : Visual
    {
        DependencyObject child = childObject as DependencyObject;
        // iteratively traverse the visual tree
        while ((child != null) && !(child is T))
        {
            child = VisualTreeHelper.GetParent(child);
        }
        return child as T;
    }
    
    public static List<T> GetVisualChildCollection<T>(object parent) where T : Visual
    {
        List<T> visualCollection = new List<T>();
        GetVisualChildCollection(parent as DependencyObject, visualCollection);
        return visualCollection;
    }
    private static void GetVisualChildCollection<T>(DependencyObject parent, List<T> visualCollection) where T : Visual
    {
        int count = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < count; i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(parent, i);
            if (child is T)
            {
                visualCollection.Add(child as T);
            }
            else if (child != null)
            {
                GetVisualChildCollection(child, visualCollection);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following control template: <ControlTemplate x:Key=GlassButton TargetType={x:Type Button}> <ControlTemplate.Resources> <Storyboard x:Key=Timeline1> <DoubleAnimationUsingKeyFrames
I have the following XAML/Control Template for a ListViewItem: <Window.Resources> <ControlTemplate x:Key=ItemTemplate TargetType=ListViewItem> <Border
I have the following ControlTemplate for a WPF TabItem: <ControlTemplate x:Key=DefaultTabItemTemplate TargetType={x:Type TabItem}> <ControlTemplate.Resources>
Is this a known issue? I have the following XAML: <Grid x:Name=LayoutRoot> <Popup> <Slider
I have the following code (only relevant snippets) <ribbon:RibbonWindow .............> <Grid> <ribbon:Ribbon> <ribbon:RibbonToggleButton x:Name=Button2
I have following situation: I have loged user, standard authentication with DB table $authAdapter
I have following string String str = replace :) :) with some other string;
I have following foreach-loop: using System.IO; //... if (Directory.Exists(path)) { foreach(string strFile in Directory.GetFiles(path,
I have following situation. A main table and many other tables linked together with
I have following table structure: Table: Plant PlantID: Primary Key PlantName: String Table: Party

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.