Home > DeveloperSection > Forums > How can i get the element from xaml to C# through Resource dictionary?
ben reitman
ben reitman

Total Post:96

Points:676
Posted on    August-16-2013 5:59 AM

 WPF WPF 
Ratings:


 1 Reply(s)
 1701  View(s)
Rate this:
Hi Developers!

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 Margin="2" Width="16"  Source="{Binding Path=SmallIcon, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"/>

                <Border Height="20" Grid.Row="1" Background="Red"/>

             </Grid>

         </Border>

         <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}"

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

    </Grid>

</Border> 

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?.



shreesh chandra shukla
shreesh chandra shukla

Total Post:105

Points:735
Posted on    August-17-2013 7:02 AM

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


Don't want to miss updates? Please click the below button!

Follow MindStick