WPF 从 ListBox 获取项目作为 ItemTemplate 的类型
我有一个列表框,如下所示:
xmlns:local="clr-namespace:MyGui.Controls"
<ListBox Grid.Row="1" ItemsSource="{Binding MyData}" x:Name="MyDataContainer">
<ListBox.ItemTemplate>
<DataTemplate>
<local:DataDisplay />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
我想枚举列表框中的项目作为类型的对象:MyGui.Controls.DataDisplay,如 ItemTemplate 中指定的。例如:
foreach (var row in MyDataContainer.Items)
if (row != null)
{
var tmp = MyDataContainer.ItemContainerGenerator.ContainerFromItem(row);
if (tmp is ListBoxItem)
{
return (tmp as ListBoxItem).PROPERTY_I_WANT as DataDisplay;
}
}
这可能吗?
I have a Listbox as so:
xmlns:local="clr-namespace:MyGui.Controls"
<ListBox Grid.Row="1" ItemsSource="{Binding MyData}" x:Name="MyDataContainer">
<ListBox.ItemTemplate>
<DataTemplate>
<local:DataDisplay />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I would like to enumerate through the items from the listbox as objects of type: MyGui.Controls.DataDisplay, as specified in the ItemTemplate. For Example:
foreach (var row in MyDataContainer.Items)
if (row != null)
{
var tmp = MyDataContainer.ItemContainerGenerator.ContainerFromItem(row);
if (tmp is ListBoxItem)
{
return (tmp as ListBoxItem).PROPERTY_I_WANT as DataDisplay;
}
}
Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这在某种程度上是可能的,大多数情况下虚拟化会阻碍对所有项目执行此操作。在这种情况下,不建议这样做,如果您需要访问
ItemTemplate
中的控件或容器上的任何内容,您应该在模板中绑定它本身或ItemContainerStyle
。This is possible to some degree, most often virtualization will thwart efforts to do this for all items. This being the case it is not recommended, if there is anything you need to access on a control in the
ItemTemplate
or on the container you should bind it, either in the template itself or theItemContainerStyle
.