首页 文章

WPF ListBox数据绑定和事件

提问于
浏览
1

我的问题很简单 .
我有一个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>

我想显示一个图像,但作为一个新的StackOverFlow用户,我不能 . 你可以在这里找到图像:

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

(对于那些不信任我的人,我在这里解释图像的内容:左边是缩略图列表(垂直显示),右边有一个更大的图像,默认对应一个大图像第一个缩略图) .

当我点击缩略图(左侧)时,右侧的大图像应该由我点击的图像更新 .

由于我是WPF的新手,我的方法可能与ListBox完全错误 . 请WPF大师告诉我光明!

1 回答

  • 4

    我想,你可以在ListBox上使用事件,像SelectionChanged一样......但这完全不是真正的WPF-Jedi方式 - 记住,代码隐藏是黑暗的一面! =)

    认为数据绑定,'s the Force. Bind your large Image element'的源是 ListBoxSelectedItem 属性 . 应该是这样的

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

    附:每个WPF-databinding-jedi都应该附近this cheat sheet .

    P.P.S.实际上,当您将StackPanel作为所选项目时......在这种情况下,您可以尝试the SelectedValuePath trick,将其设置为"absolutePath"并将大图像绑定到 SelectedValue 属性 .

    所以你的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}">
    

相关问题