WPF - 将列表框绑定到图像的 ObservableCollection

发布于 2024-10-21 15:20:18 字数 1300 浏览 6 评论 0原文

这应该非常简单,但我无法让它工作...

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

素食主义者 2024-10-28 15:20:18

您发布的代码看起来不错,所以问题一定是其他问题

  • ListBox 是否具有正确的 DataContext,以便它可以正确绑定到 AvailableItems
  • 您如何在代码中初始化您的Image

示例

BitmapImage source = new BitmapImage();
source.BeginInit();
source.UriSource = new Uri(yourUriString, UriKind.RelativeOrAbsolute);
source.EndInit();

ThumbnailImage = new Image();
ThumbnailImage.Source = source;

我将您的代码粘贴到示例项目中并且运行良好,将其上传到此处
http://www.mediafire.com/download.php?m99kv1uglrr31j9

将其与您的版本以查看您缺少的内容

The code you posted seems fine so the problem must be something else

  • Does the ListBox have the correct DataContext so it can properly bind to AvailableItems?
  • How are you initializing your Image in code?

Example

BitmapImage source = new BitmapImage();
source.BeginInit();
source.UriSource = new Uri(yourUriString, UriKind.RelativeOrAbsolute);
source.EndInit();

ThumbnailImage = new Image();
ThumbnailImage.Source = source;

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

匿名的好友 2024-10-28 15:20:18

我猜你要么:
1)没有初始化你的ObservableCollection
2)没有设置您的窗口的DataContext,或者
3)您在将 CustomItem 添加到 ObservableCollection 后设置图像,并且尚未在 CustomItem 类上实现 INotifyPropertyChanged。

你的代码加上这个似乎对我有用:

    public MainWindow()
    {
        InitializeComponent();
        this.AvailableItems = new ObservableCollection<CustomItem>();
        Image i = new Image();
        BitmapImage src = new BitmapImage();
        src.BeginInit();
        src.UriSource = new Uri(@"C:\Users\Public\Pictures\Sample Pictures/Desert.jpg");
        src.EndInit();
        i.Source = src;
        i.Stretch = System.Windows.Media.Stretch.Fill;

        CustomItem ci = new CustomItem();
        ci.ThumbnailImage = i;

        this.AvailableItems.Add(ci);
    }


    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        this.DataContext = this;
    }

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:

    public MainWindow()
    {
        InitializeComponent();
        this.AvailableItems = new ObservableCollection<CustomItem>();
        Image i = new Image();
        BitmapImage src = new BitmapImage();
        src.BeginInit();
        src.UriSource = new Uri(@"C:\Users\Public\Pictures\Sample Pictures/Desert.jpg");
        src.EndInit();
        i.Source = src;
        i.Stretch = System.Windows.Media.Stretch.Fill;

        CustomItem ci = new CustomItem();
        ci.ThumbnailImage = i;

        this.AvailableItems.Add(ci);
    }


    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        this.DataContext = this;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文