DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Unlocking the Potential of Binary Search Trees with C# Programming
  • Building Call Graphs for Code Exploration Using Tree-Sitter
  • Two-Pass Huffman in Blocks of 2 Symbols: Golang Implementation
  • Implementing LSM Trees in Golang: A Comprehensive Guide

Trending

  • Endpoint Security Controls: Designing a Secure Endpoint Architecture, Part 1
  • Building Resilient Networks: Limiting the Risk and Scope of Cyber Attacks
  • Building Resilient Identity Systems: Lessons from Securing Billions of Authentication Requests
  • Unlocking Data with Language: Real-World Applications of Text-to-SQL Interfaces

How to Access a Named Control Inside a XAML DataTemplate (using CSharp)

By 
Jerry Nixon user avatar
Jerry Nixon
·
Sep. 11, 12 · Interview
Likes (0)
Comment
Save
Tweet
Share
35.5K Views

Join the DZone community and get the full member experience.

Join For Free

Let me summarize the problem

In XAML, the ItemsControl, ListBox, ListView, FlipView, or GridView are the repeater controls. To define how each item is displayed in the UI, the developer defines a DataTemplate which he assigns to the ItemsControl’s ItemTemplate property. The ItemTemplate is like this:

The ItemTemplate

<ItemTemplate>
<DataTemplate>
<TextBox x:Name=”TextBox1” />

So far, so easy. This is a common scenario and necessary for any repeater control in XAML. Rendering the ItemsControl, the ItemTemplate is repeated (see below) for every item in the ItemsSource (which has been assigned to some enumerable value like IList, ObservableCollection or even an Array).

Note: It is possible to vary what ItemTemplate is used per-item using the ItemTemplateSelector but for the sake of this example (and simplicity) I’m just talking about the vanilla scenario.

The User Interface

<TextBox x:Name=”TextBox1” />
<TextBox x:Name=”TextBox1” />
<TextBox x:Name=”TextBox1” />
<TextBox x:Name=”TextBox1” />
<TextBox x:Name=”TextBox1” />
<TextBox x:Name=”TextBox1” />

So far so good. Your data is displaying. So, what’s the problem? See the name of the TextBox? It’s TextBox1 over and over and over. To make this work, the scope of a template is unique to the page. This means referencing a TextBox inside a DataTemplate by name is impossible. To do so, we must spelunk into the ItemTemplate’s DataTemplate.

How do you access controls inside a Template?

There is more than one approach. One approach, and my least favorite, is using the Loaded RoutedEvent on the TextBox control itself. In this approach you store the resulting (sender) in some dictionary. The up-side is that this event is handled on the page. The downside is how unmanageable it becomes if there are many objects or many different objects.

Spelunking with the Visual Tree

The preferred approach, to me, is navigating through the visual tree of the  individual item. With this you are effectively changing your scope into the individual item. From there you interact with it like you expect. I realize it’s not as simple as Loaded, but ‘tis far more repeatable, understandable and maintainable, and cleaner. Here’s how you do it:

image

In the code above, I loop through and find “TextBox2” in every item in the repeater. Just as easily you could look at just one item or just the SelectedItem – whatever best suited your use case. In either case, the trick is leveraging AllChildren() a custom method I wrote to retrieve all the children in a given scope. Here’s the code for that method:

Using VisualTreeHelper

image

In the code above, I iterate through the values in the VisualTreehelper’s GetChild() method. This method, in concert with GetChildrenCount(), let’s me easily move around the tree.

This is a powerful method. You can see that the heavy lifting is accomplished with the VisualTreehelper. This helper lets you interact with the Visual Tree. The Visual Tree should not be confused with the Logical Tree. The Logical Tree is, basically, the XAML the developer types into the application. The Visual Tree represents those same controls and all the nested controls necessary to render the correct UI.

For example, the Logic Tree is <Button /> but the Visual Tree includes the border, the background, the TextBlock and anything else necessary to draw the button. As a result, searching for a control in the Visual Tree is almost always a guarantee that you will find it, if it exists.

Conclusion

Interacting with XAML controls is complex //until// you understand how XAML works. Only an idiot would say there is no learning curve. But only an idiot would say it is insurmountable for an average developer to master the XAML engine in a very short time. What’s beautiful about XAML is how logical and straight-forward it is. What’s beautiful about XAML is how powerful it is. And, what’s beautiful about XAML is how reusable those skills are across MS platforms.

Best of luck!

Tree (data structure) csharp

Published at DZone with permission of Jerry Nixon, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Unlocking the Potential of Binary Search Trees with C# Programming
  • Building Call Graphs for Code Exploration Using Tree-Sitter
  • Two-Pass Huffman in Blocks of 2 Symbols: Golang Implementation
  • Implementing LSM Trees in Golang: A Comprehensive Guide

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!