views:

28

answers:

1

Hi,

I am quite new to WPF and have a basic question : Assume the following is xaml declaration in my xaml file

<ContentControl x:Name="interactionActivityContent" Loaded="interactionActivityContent_Loaded">
<shapes:BaseShape.DragThumbTemplate >
    <ControlTemplate x:Name="interactionActivityTemplate">
        <Grid AllowDrop="True" x:Name="GridTest" >
            <Rectangle Name="Interaction" Fill="Yellow" Stroke="Green" StrokeThickness="2" IsHitTestVisible="True" AllowDrop="True"></Rectangle>
            <local:DesignerCanvas x:Name="ActivitiesCanvasArea" Margin="1,1,1,1" IsHitTestVisible="True" AllowDrop="True" Background="Blue"></local:DesignerCanvas>
        </Grid>
    </ControlTemplate>
</shapes:BaseShape.DragThumbTemplate>


*shapes:BaseShape.DragThumbTemplate is coming from some different class. *DesignerCanvas is my own custom canvas for which I want to set value at run time. How can I access ActivitiesCanvasArea in my C# code from the code behind file? Do I need to change the way xaml is declared. I need to apply DragThumbTemplate to my grid so that I can move around grid on main screen.

A: 

From http://blogs.msdn.com/b/wpfsdk/archive/2007/03/16/how-do-i-programmatically-interact-with-template-generated-elements-part-i.aspx

If you need to find a named element in the ControlTemplate of myButton1, like the Grid, you can use Template.FindName like so:

// Finding the grid generated by the ControlTemplate of the Button Grid gridInTemplate = (Grid)myButton1.Template.FindName("grid", myButton1);

Turntwo