如何以编程方式访问 ContentTemplate 中定义的元素? [英] How can I programmatically access elements defined in a ContentTemplate?

查看:28
本文介绍了如何以编程方式访问 ContentTemplate 中定义的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我已经使用 XAML 中定义的以下 ContentTemplate 创建了一个 UserControl:

Let's say I've created a UserControl with the following ContentTemplate defined in XAML:

<UserControl.ContentTemplate>
    <DataTemplate> 
        <Ellipse Name="myEllipse" Stroke="White"/>
        <ContentPresenter Content="{TemplateBinding Content}"/>
    </DataTemplate>
</UserControl.ContentTemplate>

我如何访问代码中的myEllipse"元素,例如,我可以通过myEllipse.Height"找到它的高度?我无法直接按名称访问它.我尝试使用以下方法创建对它的引用:

How would I access the "myEllipse" element within my code so that, for example, I could find its height with "myEllipse.Height"? I cannot access it by name directly. I attempted to create a reference to it with:

Ellipse ellipse = ContentTemplate.FindName("myEllipse",this) as Ellipse;  

当我运行程序时它崩溃了,说它无法创建我的类的实例.也许我没有正确使用 FindName.如果有人能帮助我,我将不胜感激.

It crashes when I run the program, saying it can't create an instance of my class. Perhaps I'm not using FindName correctly. If anyone can help me out it would be much appreciated.

谢谢,

达拉尔

推荐答案

为了在 DataTemplate 上使用 FindName,您需要引用 ContentPresenter.请参阅 Josh Smith 的文章 如何使用 FindName带有内容控件.

In order to use FindName on a DataTemplate, you will need a reference to the ContentPresenter. See Josh Smith's article How to use FindName with a ContentControl.

您可能真正想做的是使用 ControlTemplate 而不是 DataTemplate.这应该更易于使用,并且可以让控件的用户应用他们自己的内容模板或使用隐式模板.如果你这样做:

What you may actually want to do is to use a ControlTemplate rather than a DataTemplate. This should be easier to use and will let users of your control apply their own content templates or use implicit templates. If you do something like this:

<UserControl.Template>
    <ControlTemplate TargetType="UserControl">
        <Grid>
            <ContentPresenter/>
            <Ellipse Name="myEllipse" Stroke="White"/>
        </Grid>
    </ControlTemplate>
</UserControl.Template>

然后在代码中(可能在 OnApplyTemplate 覆盖中)你将能够做到这一点:

Then in code (perhaps in an OnApplyTemplate override) you will be able to do this:

var ellipse = Template.FindName("myEllipse", this) as Ellipse;

你还应该像这样用 TemplatePartAttribute 装饰你的类:

You should also decorate your class with a TemplatePartAttribute like this:

[TemplatePart(Name="myEllipse", Type = typeof(Ellipse))]

这样,如果有人重新模板化您的控件,他们就会知道提供具有该名称的 Ellipse 元素.(如果该类仅在内部使用,则这不太重要.)

So that if anyone re-templates your control they know to provide an Ellipse element with that name. (This is less important if the class is only used internally.)

最后,如果您只想更改椭圆的颜色,那么您可能只想使用数据绑定.您可以在您的控件上创建一个 EllipseColor 依赖属性,然后设置 Stroke="{TemplateBinding EllipseColor}".

Finally, if all you want to do is change the color of the Ellipse, then you may just want to use data binding. You could create an EllipseColor dependency property on your control and just set Stroke="{TemplateBinding EllipseColor}".

这篇关于如何以编程方式访问 ContentTemplate 中定义的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆