WPF - 将列表框绑定到图像的 ObservableCollection
这应该非常简单,但我无法让它工作...
CustomItem 是一个具有名为 ThumbnailImage 属性的类 我正在尝试将 ObservableCollection 绑定到 ListBox 以显示图像。这是我的代码:
public ObservableCollection<CustomItem> AvailableItems { get; set; }
<ListBox Width="103" Height="480" ItemsSource="{Binding AvailableItems}">
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Black" BorderThickness="1">
<ContentControl Content="{Binding Path=ThumbnailImage}"
Width="100" Height="100" />
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
CustomItem 看起来像这样,
public class CustomItem
public Image ThumbnailImage { get; set; }
}
当我运行它时,ListBox 中没有显示任何内容。知道出了什么问题吗?谢谢!
-- 编辑 1 -- 我想我从调试中可以看出的是,当 AvailableItems.Count == 5 时,closet.Items.Count == 0。我尝试添加 ItemsSource="{Binding availableItems, UpdateSourceTrigger =PropertyChanged}”但这没有帮助:(
-- 编辑 2 --
我在 XAML 中执行以下操作,
DataContext="{Binding RelativeSource={RelativeSource Self}}"
而不是在代码隐藏中执行以下操作时,一切正常:
DataContext = this;
This should be extremely simple, but I jut can't get it to work...
CustomItem is a class that has a property called ThumbnailImage
I am trying to bind an ObservableCollection to a ListBox to display images. This is my code:
public ObservableCollection<CustomItem> AvailableItems { get; set; }
<ListBox Width="103" Height="480" ItemsSource="{Binding AvailableItems}">
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Black" BorderThickness="1">
<ContentControl Content="{Binding Path=ThumbnailImage}"
Width="100" Height="100" />
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
The CustomItem looks like this
public class CustomItem
public Image ThumbnailImage { get; set; }
}
Nothing is showing up in the ListBox when I run it. Any idea what's going wrong? Thanks!
-- Edit 1 -- I guess what I can tell from debugging is that closet.Items.Count == 0 when AvailableItems.Count == 5. I tried adding ItemsSource="{Binding AvailableItems, UpdateSourceTrigger=PropertyChanged}" but that didn't help :(
-- Edit 2 --
I was doing the following in my XAML
DataContext="{Binding RelativeSource={RelativeSource Self}}"
instead when I did the following in the codebehind, everything worked:
DataContext = this;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您发布的代码看起来不错,所以问题一定是其他问题
ListBox
是否具有正确的 DataContext,以便它可以正确绑定到AvailableItems
?Image
?示例
我将您的代码粘贴到示例项目中并且运行良好,将其上传到此处
http://www.mediafire.com/download.php?m99kv1uglrr31j9
将其与您的版本以查看您缺少的内容
The code you posted seems fine so the problem must be something else
ListBox
have the correct DataContext so it can properly bind toAvailableItems
?Image
in code?Example
I pasted your code into a sample project and it worked fine, uploaded it here
http://www.mediafire.com/download.php?m99kv1uglrr31j9
Compare it to your version to see what you're missing
我猜你要么:
1)没有初始化你的ObservableCollection
2)没有设置您的窗口的DataContext,或者
3)您在将 CustomItem 添加到 ObservableCollection 后设置图像,并且尚未在 CustomItem 类上实现 INotifyPropertyChanged。
你的代码加上这个似乎对我有用:
I'd guess you either:
1)Didn't initialize your ObservableCollection
2)Didn't set the DataContext of your Window, or
3)You're setting your Images after adding your CustomItem to the ObservableCollection and you haven't implemented INotifyPropertyChanged on your CustomItem class.
Your code plus this seems to work for me: