简体   繁体   English

以编程方式从模板创建元素

[英]Create an element from template programmatically

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: 在Window标记内,我声明了名称空间:

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. 现在,我拖动该元素,将触发事件“ _event”。 If I push a standard element (eg Rectangle) through this, I do the following at the end of the drag-n-drop chain of events: 如果我通过它推动一个标准元素(例如Rectangle),则在事件拖放链的末尾执行以下操作:

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? 我该如何使用someClass的元素做最后一部分? If I do 如果我做

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

the template "templateX" isn't assigned to it. 模板“ templateX”未分配给它。

The issue in this case seems to be that you want to combine two things: 在这种情况下,问题似乎在于您想结合两件事:

  • an instance of your custom class ( new_instance ) 您的自定义类的实例( new_instance
  • a control template available as a XAML resource 可用作XAML资源的控件模板

You already know how to create the instance of your class and how to add it to the Children list. 您已经知道如何创建类的实例以及如何将其添加到“ Children列表中。

How to retrieve the control template (or for that matter, any other object) from a XAML resource has been discussed in other SO questions, eg: 其他SO问题中还讨论了如何从XAML资源中检索控制模板(或其他任何对象)的方法,例如:

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. 现在,关键的一点是,你不想控制模板本身添加到Children名单。 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. 控件模板只是一组说明,说明如何为控件创建UI树并将控件的属性绑定到控件的属性(如果适用)。

Instead, you want to configure new_instance to use the control template you retrieved from the resource. 而是要配置new_instance以使用从资源中检索到的控制模板。 You can do that by assigning the control template to the Template property of new_instance : 您可以通过将控件模板分配给new_instanceTemplate属性 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. new_instance添加到Children ,它将显示出来,并将使用您的自定义控件模板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM