简体   繁体   English

ListBox在另一个ListBox中显示选定的项目(图像)(多重选择模式)

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

I'm just trying to experiment some ListBox functionality in the SelectionChanged event. 我只是尝试在SelectionChanged事件中尝试一些ListBox功能。

I have the following controls: 我有以下控件:

1.ListBox : Name = ListBoxSource (I just added the Image in XAML) 1.ListBox:名称= ListBoxSource(我刚刚在XAML中添加了Image

2.ListBox : Name = ListBoxDisplay 2.ListBox:名称= ListBoxDisplay

I just want to iterate and get those items selected from ListBoxSource and display it to ListBoxDisplay . 我只想迭代并从ListBoxSource选择那些项目,并将其显示到ListBoxDisplay How to do that in the Loop? 如何在循环中做到这一点?

The Items on the ListBoxSource are only Image controls and no other controls. ListBoxSource上的项目只是Image控件,而没有其他控件。

I cannot find any solutions on the net because most of the examples/solutions are using TextBlock , TextBox , or CheckBox ...and no Images . 我在网上找不到任何解决方案,因为大多数示例/解决方案都使用TextBlockTextBoxCheckBox ...并且没有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. 另外:使用DataTemplate和DataBinding填充列表框,这将使此构造更加健壮和灵活。

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 对不起,我的错误是此代码应该起作用,您必须处理其他属性,例如width和height

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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