列表框,对象为图像

时间:2014-08-23 11:12:02

标签: c# wpf image listbox

我希望将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>

1 个答案:

答案 0 :(得分:0)

您好我怀疑您的问题可能是您的数据模型没有实现INotifyPropertyChanged,或者imgPath和名称是属性。或者您可能错误地将图像添加到项目中。确保它们不是资源而不是复制,或者如果您希望它们在装配中或外部,则取决于复制永不复制的内容。

这很有效,我已经改变了你的属性名称,创建了一个基本的视图模型,我想你已经描述了一个数据项集合。我已将您的商品绑定到此系列。

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>

Datamodel,请注意这里INotifyPropertyChanged的实现。也许Image路径应该是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));
    }
}