问题
I'm just trying to experiment some ListBox functionality in the SelectionChanged event.
I have the following controls:
1.ListBox : Name = ListBoxSource (I just added the Image in XAML)
2.ListBox : Name = ListBoxDisplay
I just want to iterate and get those items selected from ListBoxSource and display it to ListBoxDisplay. How to do that in the Loop?
The Items on the ListBoxSource are only Image controls and no other controls.
I cannot find any solutions on the net because most of the examples/solutions are using TextBlock, TextBox, or CheckBox ...and no Images.
foreach (Object selectedItem in ListBox1.SelectedItems)
{
// What to do in here to add the selected Images to "ListBoxDisplay"
}
回答1:
Use this
<ListBox x:Name="ListBoxDisplay"
ItemsSource="{Binding ElementName=ListBoxSource, Path=SelectedItems}"/>
instead of all that code.
Also: Use a DataTemplate and DataBinding to fill the ListBoxes that will make this construction much more robust and flexible.
回答2:
for(int i=0;i<ListBoxSource.Items.Count;i++)
{
Image currentImageItem = ListBoxSource.Items[i] as Image;
Image image = new Image();
image.Source = currentImageItem.Source ;
ListBoxDisplay.Items.Add(image);
}
Sorry for my mistake this code should work you must handle other properties like width and height
来源:https://stackoverflow.com/questions/8561678/listbox-display-selected-items-image-in-another-listbox-selection-mode-multip