Bind ListBox to List<Image>
I need to bind a List of Images to a list box. My code being:
<ListBox x:Name="lstImages">
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type Image}">
<StackPanel>
<Image Source="{Binding Path=UnassignedImages}"开发者_开发百科></Image>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Code behind:
lstImages.ItemsSource = this.audit.UnassignedImages;
Where UnassignedImages being List
I tried using both lstImages.ItemsSource & lstImages.DataContent, but none works.
Thanks.
What's the type of items in lstImages? If it's System.Windows.Controls.Image then you can get rid of the ItemTemplate completely because Image is already a UIElement that knows how to render itself. If it's something like an image path (string or Uri) or a System.Windows.Media.ImageSource you need to change DataTemplate to use this to use each item in the list as the Source for an Image:
<Image Source="{Binding}"/>
You should also remove the DataType declaration on your DataTemplate since it is not only unnecessary but also incorrect. DataType should specify the type of the data and as mentioned above, controls don't need DataTemplates to be rendered.
精彩评论