---
title: "How can i get the element from xaml to C# through Resource dictionary?"  
description: "How can i get the element from xaml to C# through Resource dictionary?"  
author: "Anonymous User"  
published: 2013-08-16  
updated: 2013-08-17  
canonical: https://www.mindstick.com/forum/1380/how-can-i-get-the-element-from-xaml-to-c-sharp-through-resource-dictionary  
category: "wpf"  
tags: ["wpf"]  
reading_time: 2 minutes  

---

# How can i get the element from xaml to C# through Resource dictionary?

Hi [Developers](https://www.mindstick.com/articles/12875/blunders-you-must-avoid-while-hiring-java-developers-to-get-the-best-fulfilling-your-needs)!

Just for an example i have the below code in template.xaml.

<Border x:Name="PART_ButtonNormal" Grid.Column="0">

<Grid>

<Grid.ColumnDefinitions>

<ColumnDefinition Width="Auto" />

<ColumnDefinition />

</Grid.ColumnDefinitions>

<Border Name="PART_ImageBorder" Grid.Column="0">

<Grid>

<Grid.RowDefinitions>

<RowDefinition Height="Auto"/>

<RowDefinition Height="Auto"/>

</Grid.RowDefinitions>

<[Image](https://www.mindstick.com/articles/23551/an-investigate-distinctive-seafood-restaurant-for-your-image-stamp-character) [Margin](https://answers.mindstick.com/qa/104990/what-is-a-margin-call)="2" Width="16" [Source](https://www.mindstick.com/interview/840/what-happens-if-the-both-source-and-destination-are-named-same)="{[Binding](https://www.mindstick.com/blog/146/dynamic-binding-in-c-sharp) Path=SmallIcon, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"/>

<Border Height="20" Grid.Row="1" [Background](https://www.mindstick.com/articles/65/how-to-change-background-color-of-tab-control-in-c-sharp)="Red"/>

</Grid>

</Border>

<TextBlock Grid.Column="1" x:Name="PART_Text" Text="{TemplateBinding [Label](https://www.mindstick.com/articles/12835/print-label-tracking-how-do-these-features-make-auspost-woocommerce-plugin-better)}"

Foreground="{TemplateBinding Foreground}"

FontFamily="{TemplateBinding FontFamily}"

FontSize="{TemplateBinding FontSize}"

VerticalAlignment="{TemplateBinding VerticalAlignment}"

HorizontalAlignment="{TemplateBinding HorizontalAlignment}"

Margin="2,0,4,0"/>

</Grid>

</Border>

I read this xaml in Wrapper.cs [class](https://www.mindstick.com/blog/165/generic-class-in-c-sharp) through [Resource](https://www.mindstick.com/blog/43433/top-best-resources-for-women-about-beauty-and-health) dictionary. Then how can i [access](https://www.mindstick.com/articles/12994/how-foreigners-can-access-blocked-websites-in-china) the Image element from template.xaml in Wrapper.cs.

Could you please any one give me the solution?.

## Replies

### Reply by shreesh chandra shukla

Hi!

Do you want the Image instance after the template is applied to an actual Control? If you are trying to modify the ControlTemplate itself before applying it to a Control, I'm not sure it is possible.

However, if you are trying to get the Image from a given Control that the template is applied to, you can just walk the visual tree:

```
public Image FindImage(Control parent){   
Queue<DependencyObject> items = new
Queue<DependencyObject>();   
items.Enqueue(parent);     while (items.Count
> 0)    {        var item =
items.Dequeue() as Visual;        if (item is
Image)            return
item;         var count =
VisualTreeHelper.GetChildrenCount(item);        for (int i =
0; i < count; ++i)           
items.Enqueue(VisualTreeHelper.GetChild(item, i));    }}
```

thanks


---

Original Source: https://www.mindstick.com/forum/1380/how-can-i-get-the-element-from-xaml-to-c-sharp-through-resource-dictionary

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
