Windows phone 8 访问DataTemplate中定义的StackPanel
我有一个LongListSelector,根据用户的选择,根据收到的数据为其选择适当的DataTemplate。 我在页面的参考资料中定义了其中3个数据模板,并在填充我的LongListSelector之前使用以下方法设置了相应的数据模板:Windows phone 8 访问DataTemplate中定义的StackPanel,windows-phone-8,datatemplate,longlistselector,Windows Phone 8,Datatemplate,Longlistselector,我有一个LongListSelector,根据用户的选择,根据收到的数据为其选择适当的DataTemplate。 我在页面的参考资料中定义了其中3个数据模板,并在填充我的LongListSelector之前使用以下方法设置了相应的数据模板: RoutesLongListSelector.ItemTemplate = Resources["SecondItemTemplate"] as DataTemplate; 在这些DataTemplates中有一个元素—StackPanel—在填充列表后,
RoutesLongListSelector.ItemTemplate = Resources["SecondItemTemplate"] as DataTemplate;
在这些DataTemplates中有一个元素—StackPanel—在填充列表后,我在其中添加了一些子元素
<StackPanel x:Name="MyStations" Grid.Column="1" Grid.Row="1">
</StackPanel>
因此,当我试图使用它的名称来添加子项[MyStations.children.add(…)]时,我得到一个错误:“MyStations”在当前上下文中不存在。
我试图在页面的ContentPanel中将其中一个模板设置为默认模板,但仍然收到相同的错误
似乎是个小问题,但我想不出什么。
有什么建议吗?您不能使用
Name
属性访问DataTemplete
的UI元素,即x:Name
。您可以使用它的加载事件来访问它。请具体说明您的要求
private void MyStations\u已加载(对象发送方,路由目标)
{
var_StackPanel=(StackPanel)发送方;
}
数据模板中的元素不能通过名称直接访问。