【问题标题】:Images aren't displayed in WPF ListBox图像不显示在 WPF 列表框中
【发布时间】:2015-09-08 04:41:51
【问题描述】:

我正在尝试在 ListView 中显示图像。这些图像是通过实体框架从 SQL Server 数据库加载的。不知何故,图像没有显示在 ListView 中,我不知道为什么。

我创建了一个test Visual Studio project

图像存储在ObservableCollection<ImageViewModel>

private ObservableCollection<ImageViewModel> images;
public ObservableCollection<ImageViewModel> Images
  {
    get { return images; }
    set { images = value; }
}

如果您需要更多详细信息,请告诉我。我很感激任何可以尝试的提示。

我会在声望达到 10 时添加截图。

ImageViewModel 类如下所示:

using Common;
using TestImage.Model;

namespace TestImage.ViewModel
{
    class ImageViewModel : ObservableObject
    {
        public ImageViewModel()
        {
            image = new ImageTable() { Name = "unknown"};
        }

        #region Properties

        private ImageTable image;
        public ImageTable Image
        {
            get { return image; }
            set { image = value; }
        }

        public string Name
        {
            get { return image.Name; }
            set
            {
                if (image.Name != value)
                {
                    image.Name = value;
                    RaisePropertyChanged("Name");
                }
            }
        }

        public byte[] ImageBinary
        {
            get { return image.ImageBinary; }
            set
            {
                if (image.ImageBinary != value)
                {
                    image.ImageBinary = value;
                    RaisePropertyChanged("ImageBinary");
                }
            }
        }

        #endregion
    }
}

MainWindow.xaml 看起来像这样:

<Window x:Class="TestImage.View.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:TestImage.ViewModel"
        Title="MainWindow" Height="375.6" Width="525">
    <Window.DataContext>
        <local:ImageLibrary/>
    </Window.DataContext>

    <Window.Resources>
        <Style TargetType="{x:Type ListBox}">

            <Setter Property="ItemTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <Border BorderBrush="Black" BorderThickness="4" CornerRadius="5" Margin="6">
                            <Image Source="{Binding Images}" Stretch="Fill" Width="100" Height="120" />
                        </Border>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="ItemsPanel">
                <Setter.Value>
                    <ItemsPanelTemplate>
                        <WrapPanel />
                    </ItemsPanelTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/>
        </Style>
    </Window.Resources>

    <Grid Margin="0,0,0.4,1.4">
        <ListBox Name="ListBoxImages" ItemsSource="{Binding Images}"  Margin="413,40,20,31.6"/>

        <Image Name="ImageSource" Source="{Binding SelectedImage}" HorizontalAlignment="Left" Height="148" Margin="14,90,0,0" VerticalAlignment="Top" Width="174"/>
        <Button Name="ButtonPick" Content="Pick" HorizontalAlignment="Left" Margin="14,266,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="0.563,-0.731"  Command="{Binding CommandPickImage}"/>
        <Button Name="ButtonSave" Content="Save" HorizontalAlignment="Left" Margin="113,266,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="0.563,-0.731" Command="{Binding CommandSaveImage}" />
        <Label Name="LabelFileName" Content="FileName" HorizontalAlignment="Left" Margin="14,40,0,0" VerticalAlignment="Top" Width="60"/>
        <TextBox Name="TextBoxFileName" HorizontalAlignment="Left" Height="26" TextWrapping="Wrap" VerticalAlignment="Top" Width="314" Margin="79,40,0,0" Text="{Binding SelectedFileName}"/>
        <Button Name="ButtonLoad" Content="Load" HorizontalAlignment="Left" Margin="113,293,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="0.563,-0.731" Command="{Binding CommandLoadImage}" />
        <Image Source="{Binding SelectedImageDatabase}" HorizontalAlignment="Left" Height="148" Margin="193,90,0,0" VerticalAlignment="Top" Width="100"/>
    </Grid>
</Window>

【问题讨论】:

  • 您还应该显示 ImageVewModel 类的声明,以及它在 ListView 中的使用方式。请在您的问题中包含您的代码和 XAML 的相关部分,而不是外部网站。
  • @Clemens:谢谢你的提示。我已将代码添加到上面的问题中。

标签: wpf data-binding listbox


【解决方案1】:

ListBox ItemTemplate 中的绑定应该是ImageViewModel 的ImageBinary 属性,而不是Images

<DataTemplate>
    <Border ...>
        <Image Source="{Binding ImageBinary}" .../>
    </Border>
</DataTemplate>

【讨论】:

    猜你喜欢
    • 2010-10-20
    • 1970-01-01
    • 2012-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多