Home » WPF

How can i get the element from xaml to C# through Resource dictionary?.

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

<Border x:Name="PART_ButtonNormal" Grid.Column="0">
                                            <ColumnDefinition Width="Auto" />
                                            <ColumnDefinition />
                                        <Border Name="PART_ImageBorder" Grid.Column="0">
                                                    <RowDefinition Height="Auto"/>
                                                    <RowDefinition Height="Auto"/>
                                                <Image Margin="2"
                                    Source="{Binding Path=SmallIcon, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"/>
                                                <Border Height="20" Grid.Row="1" Background="Red"/>
                                        <TextBlock Grid.Column="1" x:Name="PART_Text"
                                           Text="{TemplateBinding Label}"
                                           Foreground="{TemplateBinding Foreground}"
                                           FontFamily="{TemplateBinding FontFamily}"
                                           FontSize="{TemplateBinding FontSize}"
                                           VerticalAlignment="{TemplateBinding VerticalAlignment}"                          
                                           HorizontalAlignment="{TemplateBinding HorizontalAlignment}"


I read this xaml in Wrapper.cs class through Resource dictionary. Then how can i access the Image element from template.xaml in Wrapper.cs.

Could you please any one give me the solution?.


David C


2 Answers Found


Answer 2

Hi David C,

Welcome to our forum.

Based on your description, I think your main concern is how to get your Image through your ResourceDictionary, if I have misunderstood your concern, please let me know.

I think if you want to get your Image as a Source through your ResourceDictionary, why not store your Image as Resource into your ResourceDictionary, for example:

<ImageSource x:Key="someImage">Images/SomeImage.extension</ImageSource>

And then you could get it using:

Control.Source = (ImageSource)FindResource("someImage");

On the other hand, I think you coud try to define your template.xaml with a key, and then you could get it through the key, this Article demonstrating you how to use this way to achieve your goal, the link is:



Best regards,





<< Previous      Next >>

Microsoft   |   Windows   |   Visual Studio   |   Sharepoint   |   Azure