如何迭代 LibraryStack 中的元素?
我对 LibraryStack 有以下定义:
<s:LibraryStack Name="TaggingContainer" Margin="20" Grid.Row="1" Grid.ColumnSpan="2" AllowDrop="True" Height="300" Width="300" s:SurfaceDragDrop.DragLeave="TaggingContainer_DragLeave" s:SurfaceDragDrop.DragEnter="TaggingContainer_DragEnter" s:SurfaceDragDrop.PreviewDrop="LibraryStack_PreviewDrop">
<s:LibraryStack.ItemTemplate>
<DataTemplate>
<Label Content="{Binding Name}" Tag="{Binding}" FontSize="20" Margin="10,10,10,10" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="White" Background="#c49b14" BorderBrush="White" BorderThickness="2" s:Contacts.PreviewContactDown="Label_PreviewContactDown"></Label>
</DataTemplate>
</s:LibraryStack.ItemTemplate>
</s:LibraryStack>
现在,在代码隐藏中,我想迭代 LibraryStack 中包含的所有标签(数据模板中定义的标签)。
但如果我使用,
foreach (FrameworkElement element in TaggingContainer.Items) {
}
我会迭代 TaggingContainer 中的数据对象,而不是 Datatemplates。我怎样才能改变这一点?
I have the following definition of a LibraryStack:
<s:LibraryStack Name="TaggingContainer" Margin="20" Grid.Row="1" Grid.ColumnSpan="2" AllowDrop="True" Height="300" Width="300" s:SurfaceDragDrop.DragLeave="TaggingContainer_DragLeave" s:SurfaceDragDrop.DragEnter="TaggingContainer_DragEnter" s:SurfaceDragDrop.PreviewDrop="LibraryStack_PreviewDrop">
<s:LibraryStack.ItemTemplate>
<DataTemplate>
<Label Content="{Binding Name}" Tag="{Binding}" FontSize="20" Margin="10,10,10,10" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="White" Background="#c49b14" BorderBrush="White" BorderThickness="2" s:Contacts.PreviewContactDown="Label_PreviewContactDown"></Label>
</DataTemplate>
</s:LibraryStack.ItemTemplate>
</s:LibraryStack>
Now in the code-behind I want to iterate over all the labels that are contained in the LibraryStack (the label defined in the data template).
But if I use
foreach (FrameworkElement element in TaggingContainer.Items) {
}
I iterate over the data objects that are in the TaggingContainer and not the Datatemplates. How can I change that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最简单的方法可能是在可视化树中的
LibraryStack
下找到每个LibraryStackItem
或Label
。试试这个或者,您可以获取每个元素的容器,并以这种方式获取
Label
,而不是GetVisualChild
GetVisualChildCollection
The easiest way is probably to find every
LibraryStackItem
orLabel
under theLibraryStack
in the Visual Tree. Try thisAlternatively you could get the container for each element and get the
Label
that way insteadGetVisualChild
GetVisualChildCollection