1

Здравствуйте. Пытаюсь привязать коллекцию картинок к листбоксу. Код:

Модель:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Media;
using System.IO;
using System.ComponentModel;
using System.Windows.Controls;
using System.Windows.Media.Imaging;

namespace Bind
{
    class MyImage:INotifyPropertyChanged
    {
        public MyImage( string path, string descriptionTop, string descriptionBottom )
        {
            this.img = new Image();
            this.img.Source = new BitmapImage(new Uri(path));

            this.descriptionTop = descriptionTop;
            this.descriptionBottom = descriptionBottom;
            this.FullPath = path;      
        }

        Image img;
        string descriptionTop;
        string descriptionBottom;
        string full_path;

        public Image Img
        {
            get
            {
                return this.img;

            }
            set
            {
                this.img = value;
                OnPropertyChanged("Img");
            }
        }

        public string DescriptionTop
        {
            get
            {
                return this.descriptionTop;
            }
            set
            {
                this.descriptionTop = value;
                OnPropertyChanged("DescriptionTop");
            }
        }

        public string DescriptionBottom
        {
            get
            {
                return this.descriptionBottom;
            }
            set
            {
                this.descriptionBottom = value;
                OnPropertyChanged("DescriptionBottom");
            }
        }

        public string FullPath
        {
            get
            {
                return this.full_path;
            }
            set
            {
                this.full_path = value;
                OnPropertyChanged("FullPath");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string p_propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(p_propertyName));
            }
        }

    }
}

Представление:

<Window x:Class="Bind.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Name="Wind" 
        DataContext="myViewModel">
    <Window.Resources>
        <!--<DataTemplate x:Key="ImageTemplate">
            <Image Source="{Binding Path=FullPath}"/>
        </DataTemplate>-->
    </Window.Resources>
    <Grid>
        <ListBox HorizontalAlignment="Left" Name="listBox1" VerticalAlignment="Top"  
                 ItemsSource="{Binding Path=Images}"  
                 Background="Beige"
                 Width="{Binding ElementName=Wind, Path=Width}"
                 Height="{Binding ElementName=Wind, Path=Height}">
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel/>  
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Path=DescriptionTop}"/>   
                        <Image Height="65" Width="60" Source="{Binding Path=FullPath}"/>
                        <TextBlock Text="{Binding Path=DescriptionBottom}"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

View-Model:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.IO;

namespace Bind.view_model
{
    class MyViewModel:INotifyPropertyChanged
    {
        public MyViewModel()
        {
            this.Images = new ObservableCollection<MyImage>();
            //this.images = new ObservableCollection<MyImage>();
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string p_propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(p_propertyName));
            }
        }

        ObservableCollection<MyImage> images;

        public ObservableCollection<MyImage> Images
        {
            get
            {
                images = new ObservableCollection<MyImage>();
                string[] dims = { "*.jpg", "*.png" };

                foreach (string mask in dims)
                {
                    string[] addresses = Directory.GetFiles(Directory.GetCurrentDirectory(), mask);
                    int i = 0;
                    foreach (string address in addresses)
                    {
                        i++;
                        images.Add(new MyImage(address, "image" + i.ToString(), ""));
                    }
                }

                return images;
            }
            set
            {
                images = value;
                OnPropertyChanged("Images");
            }
        }
    }
}

PS: Картинки упорно не хотят отображаться. В чём же проблема?

1 ответ 1

3
<Image Source="{Binding Img}" .../> : Img а не FullPath. 

https://stackoverflow.com/questions/2123244/wpf-listview-show-image/2123280#2123280

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.