[英]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
. 我在网上找不到任何解决方案,因为大多数示例/解决方案都使用TextBlock
, TextBox
或CheckBox
...并且没有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.