简体   繁体   中英

ListBox display selected Items (Image) in another ListBox (Selection Mode Multiple)

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"
}

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM