1

I'd like to move an element from one grid into another and have a problem to assign programmatically a template to the new instance. Further, details of my attempt.

For this purpose, I create an instance of the class together with its visual appearance from the template.

Inside the Window tag I declare the namespace:

xlmns:my="clr-namespace:myNameSpace"

I have a template in resources:

<ControlTemplate x:Key="templateX">
    <StackPanel>
        <Image Source="pic.png" Width="50" Height="50"/>
    </StackPanel>
</ControlTemplate>

and place the element into the grid.

<Grid Grid.Row="2">
    <StackPanel>
        <my:someClass Template="{StaticResource templateX}" MouseMove="_event">
    </StackPanel>
</Grid>

Now, I drag the element, the event "_event" fires. If I push a standard element (e.g. Rectangle) through this, I do the following at the end of the drag-n-drop chain of events:

Rectangle new_instance = new Rectangle();
// place for rectangle's form and color
NewPlace.Children.Add(new_instance);
// place for positioning the rectangle in NewPlace canvas

How, can I do the last part with the element of someClass? If I do

someClass new_instance = new someClass();
NewPlace.Children.Add(new_instance);

the template "templateX" isn't assigned to it.

Max Li
  • 5,069
  • 3
  • 23
  • 35
  • Is your *actual* question how to retrieve a particular resource from code-behind, as discussed e.g. [here](http://stackoverflow.com/questions/618648/how-can-i-access-resourcedictionary-in-wpf-from-c-sharp-code), or [here](http://stackoverflow.com/questions/2117886/accessing-a-resource-via-codebehind-in-wpf)? – O. R. Mapper Apr 30 '15 at 19:07
  • If a get the template like this: ContentTemplate CT = (ContentTemplate).this.FindName("templateX") I can't use it in NewPlace.Children.Add() – Max Li Apr 30 '15 at 19:10
  • Do you mean `ControlTemplate`? You should not directly add a control template to the `Children` list. Instead, can you assign the control template to `new_instance.Template` before adding `new_instance`? If `someClass` is a control, that should be possible (and if it isn't, a control template is not a suitable way, anyway). – O. R. Mapper Apr 30 '15 at 19:15
  • It worked out, thanks! More precisely, the lines`someClass new_instance = someClass(); ControlTemplate template = (ControlTemplate)this.FindResource("templateX"); new_instance.Template=template;`. Would you like to post your answer to be accepted? – Max Li Apr 30 '15 at 19:36
  • Done, posted an answer with a somewhat more structured explanation. – O. R. Mapper Apr 30 '15 at 19:45

1 Answers1

2

The issue in this case seems to be that you want to combine two things:

  • an instance of your custom class (new_instance)
  • a control template available as a XAML resource

You already know how to create the instance of your class and how to add it to the Children list.

How to retrieve the control template (or for that matter, any other object) from a XAML resource has been discussed in other SO questions, e.g.:

This leads to:

ControlTemplate template = (ControlTemplate)this.FindResource("templateX");

Now, the crucial point is that you do not want to add the control template itself to the Children list. The control template is just a set of instructions how to create a UI tree for your control and bind its properties to those of your control, where appropriate.

Instead, you want to configure new_instance to use the control template you retrieved from the resource. You can do that by assigning the control template to the Template property of new_instance:

new_instance.Template = template;

Once new_instance is added to Children, it will be displayed and it will use your custom control template.

Community
  • 1
  • 1
O. R. Mapper
  • 20,083
  • 9
  • 69
  • 114