【问题标题】:Binding a list of images to a ListBox将图像列表绑定到 ListBox
【发布时间】:2011-07-11 13:36:49
【问题描述】:

我试图将图像列表绑定到列表框,但我得到的只是类型名称列表

<ListBox x:Name="PhotosListBox" ItemsSource="{Binding MyImages}" />

MyImages 是 List&lt;BitMapImages&gt;

现在它只返回System.Windows.Media.Imaging.BitmapImage 的列表,而不是显示图像

编辑 为了进一步参考,这里是最终代码。

<ListBox x:Name="PhotosListBox" ItemsSource="{Binding MyImages}" >
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Image Source="{Binding}" Stretch="Uniform"></Image>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

【问题讨论】:

    标签: c# .net wpf xaml data-binding


    【解决方案1】:

    默认会调用绑定数据类型的ToString()方法,默认返回全限定类型名。

    您应该为 ListBox 定义一个自定义 ItemTemplate

    【讨论】:

      【解决方案2】:

      您需要使用除默认值之外的ItemTemplate,以便ListBox 知道如何处理传递给它的数据类型。

      见:http://msdn.microsoft.com/en-us/library/ms742521.aspx

      【讨论】:

      • 接受了 devdigital 的回答,仅仅因为它是 1 分钟。更快,但感谢您的帮助。
      猜你喜欢
      • 2011-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-10
      • 1970-01-01
      • 1970-01-01
      • 2015-08-27
      • 1970-01-01
      相关资源
      最近更新 更多