WPF Get Items From a ListBox as the type of the ItemTemplate
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 th开发者_开发知识库e 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?
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 the ItemContainerStyle
.
精彩评论