我的问题很简单。我有一个列表框,其中包含缩略图(图像)<ListBox Name="ListBox_Thumbnails" ItemsSource="{Binding}" DataContext="{Binding Source= {StaticResource ThumbnailListSource}}" Width="120" HorizontalAlignment="Left" Margin="-1,26,0,54"><ListBox.ItemTemplate> <DataTemplate> <StackPanel> <Image Source="{Binding Path=absolutePath}" MouseLeftButtonDown=<!--?????-->/> </StackPanel> </DataTemplate></ListBox.ItemTemplate>我想显示图像,但是作为新的StackOverFlow用户,我不能显示图像。您可以在这里找到图片:http://www.freeimagehosting.net/uploads/61aa983cad.jpg(对于那些不信任我的人,我在这里说明图片的内容:左侧有一个缩略图列表(垂直显示),而右侧则有一个较大的图像,默认情况下对应于第一个缩略图的较大图像。当我单击缩略图(在左侧)时,应该通过单击的缩略图来更新右侧的大图像。由于我是WPF的新手,所以ListBox的方法可能完全错误。WPF大师们,请给我看看光! 最佳答案 我猜想,您可以在ListBox上使用事件,诸如SelectionChanged之类的东西……但这完全不是真正的WPF-Jedi方法-记住,代码背后是阴暗的一面! =)考虑数据绑定,这就是力量。将大型Image元素的源绑定到SelectedItem的ListBox属性。它看起来像<Image Source="{Binding SelectedItem.absolutePath, ElementName=ListBox_Thumbnails}">附言每个WPF-databinding-jedi应该在附近有this cheat sheet。P.P.S.实际上,当您使用ItemTemplate时,这可能无法正常工作,您将把StackPanel作为选定的项目...在这种情况下,您可以尝试the SelectedValuePath trick,将其设置为“ absolutePath”,然后将大图像绑定到属性。因此,您的ListBox开头标记变为:<ListBox Name="ListBox_Thumbnails" ItemsSource="{Binding}" DataContext="{Binding Source= {StaticResource ThumbnailListSource}}" Width="120" HorizontalAlignment="Left" Margin="-1,26,0,54" SelectedValuePath="absolutePath">您的大图片标签将变为:<Image Source="{Binding SelectedValue, ElementName=ListBox_Thumbnails}">关于c# - WPF ListBox数据绑定(bind)和事件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2206817/
10-17 00:05