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

依然范特西╮ 提交于 2019-12-25 00:40:16

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!