C# 将多个项目绑定到listview行

C# 将多个项目绑定到listview行,c#,wpf,C#,Wpf,下面是我正在尝试做的一个描述:我需要建立一个产品目录,当用户按下目录(ListView)上的“肉”按钮时,目录(ListView)中会充满肉的图片,我希望每一行包含3种产品…以下是我到目前为止所拥有的: XAML: 和MyViewModel代码: private List<Bitmap> m_ImageCollection; public MyViewModel() { LoadImages(); } void LoadImages() { m_ImageColl

下面是我正在尝试做的一个描述:我需要建立一个产品目录,当用户按下目录(ListView)上的“肉”按钮时,目录(ListView)中会充满肉的图片,我希望每一行包含3种产品…以下是我到目前为止所拥有的:

XAML:

和MyViewModel代码:

private List<Bitmap> m_ImageCollection;
public MyViewModel()
{
    LoadImages();  
}
void LoadImages()
{
    m_ImageCollection = new List<Bitmap>();
    ResourceManager rm = Properties.Resources.ResourceManager;
    ResourceSet rs = rm.GetResourceSet(new CultureInfo("en-US"), true, true);
    if (rs != null)
    {
        var images =
          from entry in rs.Cast<DictionaryEntry>()
          where entry.Value is Bitmap
          select entry;

        foreach (DictionaryEntry img in images)
        {
            if (img.Value is Bitmap)
                m_ImageCollection.Add((Bitmap)img.Value);
        }
    }
}
public List<Bitmap> ImageCollection
{
    get { return m_ImageCollection; }    
    set { m_ImageCollection = value; }
} 
私有列表m_ImageCollection;
公共MyViewModel()
{
LoadImages();
}
void LoadImages()
{
m_ImageCollection=新列表();
ResourceManager rm=Properties.Resources.ResourceManager;
ResourceSet rs=rm.GetResourceSet(新文化信息(“en-US”),true,true;
如果(rs!=null)
{
var图像=
从rs.Cast()中的条目开始
其中entry.Value为位图
选择条目;
foreach(字典在图像中输入img)
{
if(img.Value为位图)
m_ImageCollection.Add((位图)img.Value);
}
}
}
公共列表图像集合
{
获取{return m_ImageCollection;}
设置{m_ImageCollection=value;}
} 
图片正在加载,但一行中的每一张图片我希望一行中有三张图片


有什么帮助吗?

我只需要创建一个表示行的VM:

    //immutable - so safe to bind to without INotifyPropertyChange support
    public sealed class BitmapRow
    {
            private readonly Bitmap _bitmap1;
            private readonly Bitmap _bitmap2;
            private readonly Bitmap _bitmap3;

            public BitmapRow(Bitmap bitmap1, Bitmap bitmap2, Bitmap bitmap3)
            {
                _bitmap1 = bitmap1;
                _bitmap2 = bitmap2;
                _bitmap3 = bitmap3;
            }

            public Bitmap Bitmap1{ get{return _bitmap1;}}
            public Bitmap Bitmap2{ get{return _bitmap2;}}
            public Bitmap Bitmap3{ get{return _bitmap3;}}
    }
然后:


对主虚拟机的更改如下:(顺便说一句,
List
对WPF不是一个好主意,请使用
observetecollection
):

private只读ObservableCollection m_ImageCollection=new ObservableCollection();
公共IEnumerable图像集合
{
获取{return m_ImageCollection;}
}
void LoadImages()
{            
ResourceManager rm=Properties.Resources.ResourceManager;
ResourceSet rs=rm.GetResourceSet(新文化信息(“en-US”),true,true;
如果(rs!=null)
{
//var allImages=新列表();
//var图像=
//从rs.Cast()中的条目开始
//其中entry.Value为位图
//选择条目;
//foreach(字典在图像中输入img)
//{
//var bmp=img.Value作为位图;//一种类型强制转换
//如果(bmp!=null)
//    {
//allImages.Add(bmp);
//    }
//}
//上述所有代码的最佳替代方案:
var allImages=rs.Cast().Values.OfType().ToList();
//或者你需要这样做:
var allImages=rs.Cast().Select(d=>d.Value).OfType().ToList();
//现在将3秒钟内的所有图像加载到位图行中,并添加到m_ImageCollection
//我将把这一点留给你来完成——你需要决定做什么
//当列表不是3的精确倍数时
var max=allImages.Count();
对于(ini行=0;行
private List<Bitmap> m_ImageCollection;
public MyViewModel()
{
    LoadImages();  
}
void LoadImages()
{
    m_ImageCollection = new List<Bitmap>();
    ResourceManager rm = Properties.Resources.ResourceManager;
    ResourceSet rs = rm.GetResourceSet(new CultureInfo("en-US"), true, true);
    if (rs != null)
    {
        var images =
          from entry in rs.Cast<DictionaryEntry>()
          where entry.Value is Bitmap
          select entry;

        foreach (DictionaryEntry img in images)
        {
            if (img.Value is Bitmap)
                m_ImageCollection.Add((Bitmap)img.Value);
        }
    }
}
public List<Bitmap> ImageCollection
{
    get { return m_ImageCollection; }    
    set { m_ImageCollection = value; }
} 
    //immutable - so safe to bind to without INotifyPropertyChange support
    public sealed class BitmapRow
    {
            private readonly Bitmap _bitmap1;
            private readonly Bitmap _bitmap2;
            private readonly Bitmap _bitmap3;

            public BitmapRow(Bitmap bitmap1, Bitmap bitmap2, Bitmap bitmap3)
            {
                _bitmap1 = bitmap1;
                _bitmap2 = bitmap2;
                _bitmap3 = bitmap3;
            }

            public Bitmap Bitmap1{ get{return _bitmap1;}}
            public Bitmap Bitmap2{ get{return _bitmap2;}}
            public Bitmap Bitmap3{ get{return _bitmap3;}}
    }
<StackPanel Orientation="Horizontal">
  <Image Source="{Binding Path=Bitmap1,Converter={StaticResource ImageConverter}}" Height="50" Width="100" />
  <Image Source="{Binding Path=Bitmap2,Converter={StaticResource ImageConverter}}" Height="50" Width="100" />
  <Image Source="{Binding Path=Bitmap3,Converter={StaticResource ImageConverter}}" Height="50" Width="100" />
</StackPanel>
    private readonly ObservableCollection<BitmapRow> m_ImageCollection = new ObservableCollection<BitmapRow>();
    public IEnumerable<BitmapRow> ImageCollection
    {
        get { return m_ImageCollection; }
    }

    void LoadImages()
    {            
        ResourceManager rm = Properties.Resources.ResourceManager;
        ResourceSet rs = rm.GetResourceSet(new CultureInfo("en-US"), true, true);
        if (rs != null)
        {
            //var allImages = new List<Bitmap>();
            //var images =
            //  from entry in rs.Cast<DictionaryEntry>()
            //  where entry.Value is Bitmap
            //  select entry;                

            //foreach (DictionaryEntry img in images)
            //{
            //    var bmp = img.Value as Bitmap; //one type cast
            //    if (bmp!=null)
            //    {
            //        allImages.Add(bmp);
            //    }
            //}

            //neat alternative to all of above code:
            var allImages = rs.Cast<DictionaryEntry>().Values.OfType<Bitmap>().ToList();
            //or maybe you need to do this:
            var allImages = rs.Cast<DictionaryEntry>().Select(d=>d.Value).OfType<Bitmap>().ToList();

            //now load the images in allImages in 3s into BitmapRows and add to m_ImageCollection 
            //I'll leave this bit up to you to complete - you need to decide what to do
            //when the list is not an exact multiple of 3
            var max = allImages.Count();
            for(ini row = 0; row < max/3; row+=1)
            {
               Bitmap a = allImages[row*3];
               Bitmap b = allImages[row*3+1]; //TODO: needs index checking
               Bitmap c = allImages[row*3+2]; //TODO: needs index checking
               m_ImageCollection.Add(new BitmapRow(a,b,c));
            }
        }
    }