简体   繁体   English

WPF ListBox数据绑定和事件

[英]WPF ListBox Databinding & Events

My problem is rather simple. 我的问题很简单。
I have a ListBox, containing Thumnails (Image) 我有一个ListBox,包含Thumnails(图片)

<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>

I wanted to show an image, but as a new StackOverFlow user, I can't. 我想显示一个图像,但作为一个新的StackOverFlow用户,我不能。 You can find the image here: 你可以在这里找到图像:

http://www.freeimagehosting.net/uploads/61aa983cad.jpg http://www.freeimagehosting.net/uploads/61aa983cad.jpg

(For those who don't trust me, I explain here the content of the image: On the left, there is a list of thumbnails (displayed vertically) and on the right there is a bigger image, corresponding per default to a large image of the first thumbnail). (对于那些不信任我的人,我在这里解释一下图像的内容:左边是缩略图列表(垂直显示),右边是一个更大的图像,默认对应一个大图像第一个缩略图)。

When I click on a thumbnail (on the left), the large image on the right should be updated by the one that I clicked on. 当我点击缩略图(左侧)时,右侧的大图像应该由我点击的图像更新。

As I am new with WPF, my approach is perhaps totally wrong with the ListBox. 由于我是WPF的新手,我的方法可能是ListBox的完全错误。 Please, WPF Gurus, show me the light! 请WPF大师告诉我光明!

I guess, you can use events on ListBox, smth like SelectionChanged... but that's totally not the TRUE WPF-Jedi way -- remember, code-behind is the dark side! 我想,你可以在ListBox上使用事件,像SelectionChanged一样......但这完全不是真正的WPF-Jedi方式 - 记住,代码隐藏是黑暗的一面! =) =)

Think data binding, that's the Force. 想想数据绑定,就是力量。 Bind your large Image element's source to the SelectedItem property of the ListBox . 将大型Image元素的源绑定到ListBoxSelectedItem属性。 It should look like 应该是这样的

<Image Source="{Binding SelectedItem.absolutePath, ElementName=ListBox_Thumbnails}">

PS Every WPF-databinding-jedi should have this cheat sheet nearby. PS每个WPF-databinding-jedi都应附近有这张备忘单

PPS Actually, as you're using ItemTemplate this might not work, you'll have your StackPanel as the selected item... in this case you can try the SelectedValuePath trick , set it to "absolutePath" and bind the large image to the SelectedValue property. PPS实际上,当你使用ItemTemplate这可能不起作用时,你将把你的StackPanel作为所选项目......在这种情况下你可以尝试SelectedValuePath技巧 ,将它设置为“absolutePath”并将大图像绑定到SelectedValue属性。

So your ListBox opening tag becomes: 所以你的ListBox开始标记变成:

<ListBox Name="ListBox_Thumbnails" ItemsSource="{Binding}" DataContext="{Binding Source= {StaticResource ThumbnailListSource}}" Width="120" HorizontalAlignment="Left" Margin="-1,26,0,54" SelectedValuePath="absolutePath">

And your large image tag becomes: 您的大图像标记变为:

<Image Source="{Binding SelectedValue, ElementName=ListBox_Thumbnails}">

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

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