如何在Silverlight UserControl中引用我添加到DataPager的ControlTemplate的TextBox?

时间:2022-06-12 16:18:36

Using the Silverlight 3 Release, I created a Silverlight UserControl that contains a DataPager and a DataGrid. I modified the DataPager ControlTemplate and added a TextBox with an x:Name of TestTextBox inside the DataPager's Border right before the standard DataPager buttons, but I can't seem to get a reference to that TextBox in the UserControl code behind.

使用Silverlight 3 Release,我创建了一个包含DataPager和DataGrid的Silverlight UserControl。我修改了DataPager ControlTemplate并在标准DataPager按钮之前的DataPager边框内添加了一个带有x:Name of TestTextBox的TextBox,但我似乎无法在后面的UserControl代码中获得对该TextBox的引用。

In the code behind (C#), I tried: TextBox myTextBox = this.GetTemplateChild("TestTextBox") as TextBox, but myTextBox winds up null. I suspect I need to operate against the DataPager instance, but I don't see any methods for drilling into the DataPager's child controls.

在后面的代码(C#)中,我试过:TextBox myTextBox = this.GetTemplateChild(“TestTextBox”)作为TextBox,但myTextBox结束为null。我怀疑我需要对DataPager实例进行操作,但是我没有看到任何钻入DataPager的子控件的方法。

I don't have much experience with ControlTemplates so I'm not exactly sure how to phrase the issue well enough to turn up anything useful searching here and the other usual resources. Can someone nudge me in the right direction?

我没有太多使用ControlTemplates的经验,所以我不确定如何很好地表达这个问题,以便在这里搜索任何有用的搜索和其他常用资源。有人可以朝着正确的方向推动我吗?

1 个解决方案

#1


I guess you're looking for FindControl("name")? Try it out:

我猜你在寻找FindControl(“名字”)?试试看:

TextBox tb = DataPager1.FindControl("TestTextBox") as TextBox;

At least that's how it rolls in ASP-NET, when you have a control inside a template or something and you can't access it directly.

至少这是它在ASP-NET中滚动的方式,当你在模板内部有一个控件或者你无法直接访问它时。

#1


I guess you're looking for FindControl("name")? Try it out:

我猜你在寻找FindControl(“名字”)?试试看:

TextBox tb = DataPager1.FindControl("TestTextBox") as TextBox;

At least that's how it rolls in ASP-NET, when you have a control inside a template or something and you can't access it directly.

至少这是它在ASP-NET中滚动的方式,当你在模板内部有一个控件或者你无法直接访问它时。