列表框,对象为图像

本文关键字:图像 对象 列表 | 更新日期: 2023-09-27 18:19:15

我想将observablecollection中的对象显示为带有标签的图像。对象包含名称和图像路径作为字符串。我的问题是这些图像不在解中。我创建了类似这样的东西,但它以行显示对象,没有图像。我想把它们放在盒子里。

我编辑我的源代码:

<ListBox Name="lb_drinki" HorizontalAlignment="Left" VerticalAlignment="Top" Height="650" Width="1254" Margin="10,10,0,0" 
                         ItemTemplate="{StaticResource MyImagesItemTemplate}"/> 
...
<UserControl.Resources>
    <DataTemplate x:Key="MyImagesItemTemplate">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="auto" />
                <RowDefinition Height="auto" />
            </Grid.RowDefinitions>
            <Image Grid.Row="0" Source="{Binding imgpath}" />
            <Label Grid.Row="1" Content="{Binding name}" />
        </Grid>
    </DataTemplate>
</UserControl.Resources>

列表框,对象为图像

嗨,我怀疑你的问题可能是你的数据模型没有实现INotifyPropertyChanged,或者imgPath和name是属性。或者您可能错误地将图像添加到项目中。确保它们是Resource而不是copy,或者是content with if copy never,这取决于您是希望它们在程序集中还是在程序集中。

这是有效的,我已经改变了你的属性名称,创建了一个基本的视图模型,与你所描述的数据项的集合,我认为。我已经把你的物品和这个集合绑定了。

XAML:

<Window x:Class="LBImage.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:lbImage="clr-namespace:LBImage"
    Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
    <lbImage:MainViewModel/>
</Window.DataContext>
<Window.Resources>               
<DataTemplate x:Key="MyImagesItemTemplate">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"  />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Image Grid.Row="0" Source="{Binding ImagePath}" MaxHeight="100" />
        <Label Grid.Row="1" Content="{Binding Name}" />
    </Grid>
</DataTemplate>
</Window.Resources>
<Grid>
    <ListBox HorizontalAlignment="Left" VerticalAlignment="Top" Height="650" Width="1254" Margin="10,10,0,0" 
             ItemsSource ="{Binding Items}" SelectedItem="{Binding SelectedItem,Mode=TwoWay}"
             ItemTemplate="{StaticResource MyImagesItemTemplate}"/>
</Grid>

数据模型,注意这里INotifyPropertyChanged的实现。也许图像路径应该是一个URI类型?

public class ModelBase
{
    public event PropertyChangedEventHandler PropertyChanged;
    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}
public class SomeItem : ModelBase
{
    private string name;
    private string imagePath;
    public String Name
    {
        get { return name; }
        set
        {
            if (value == name) return;
            name = value;
            OnPropertyChanged("Name");
        }
    }
    public String ImagePath
    {
        get { return imagePath; }
        set
        {
            if (value == imagePath) return;
            imagePath = value;
            OnPropertyChanged("ImagePath");
        }
    }
}

最后是你的viewmodel:

public class MainViewModel : INotifyPropertyChanged
{
    // assuming you have the images
    private ObservableCollection<SomeItem> items = new ObservableCollection<SomeItem>
    {
        new SomeItem{ImagePath = "Images/a.jpg", Name= "sdfgsdfgsdfgdfsg"},
        new SomeItem{ImagePath = "Images/b.jpg", Name= "sdfgsdfgsdfgsdfg"},
        new SomeItem{ImagePath = "Images/c.jpg", Name= "sdfsdfgsdfgsdfgsdfgssdfg"},
    };
    private SomeItem selectedItem;
    public ObservableCollection<SomeItem> Items
    {
        get { return items; }
        set
        {
            if (Equals(value, items)) return;
            items = value;
            OnPropertyChanged("Items");
        }
    }
    public SomeItem SelectedItem
    {
        get { return selectedItem; }
        set
        {
            if (Equals(value, selectedItem)) return;
            selectedItem = value;
            OnPropertyChanged("SelectedItem");
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}