【发布时间】:2011-12-19 12:56:05
【问题描述】:
我只是想在SelectionChanged 事件中试验一些ListBox 功能。
我有以下控件:
1.ListBox : Name = ListBoxSource(我刚刚在 XAML 中添加了Image)
2.ListBox : 名称 = ListBoxDisplay
我只想迭代并从ListBoxSource 中选择这些项目并将其显示到ListBoxDisplay。如何在循环中做到这一点?
ListBoxSource上的Item只有Image控件,没有其他控件。
我在网上找不到任何解决方案,因为大多数示例/解决方案都使用TextBlock、TextBox 或CheckBox ...而没有Images。
foreach (Object selectedItem in ListBox1.SelectedItems)
{
// What to do in here to add the selected Images to "ListBoxDisplay"
}
【问题讨论】:
-
你不应该需要一个循环。您是否尝试简单地将 ListBoxDisplay.Items 绑定到 ListBoxSource.SelectedItems?
-
@Martin:是的,我确实试过那个。 DataContext="{Binding ElementName=ListBoxSource, Path=SelectedItems}" 但“ListBoxDisplay”上没有显示
-
@H.B:是的..我的意思是即使示例使用 TextBlock 或其他控件,我也很难理解,因为我认为它可以显示一个字符串,这是这些控件的默认值。我的是图片
-
你做了一个错误的假设,这些项目绝对可以是任何东西,如果它是可以显示的东西,ListBox 就会显示它。
-
使用:ItemsSource="{Binding ElementName=ListBoxSource, Path=SelectedItems}" 而不是 DataContext="..."
标签: c# .net wpf silverlight