图像未显示在WPF ListBox中

时间:2015-06-22 16:14:17

标签: wpf data-binding listbox

我试图在ListView中显示图像。图像通过SQL Server数据库中的Entity Framework加载。不知怎的,图像不会显示在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>

1 个答案:

答案 0 :(得分:1)

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

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