C# - WPF GridView仅在显示时引用第一个项目

时间:2016-11-17 20:13:29

标签: c# wpf gridview observablecollection

我最近开始使用C#,WPF和Windows编程。

我目前有一个UserControl,它定义了一个绑定到ObservableCollection的Gridview。我可以使用_items.Add(newItem);向它添加新项目。但是在运行时,GridView显示正确的行数,但它们都只引用ObservableCollection中的第一个对象。见到这里:

Desktop Screenshot

上面你可以看到每一行只显示实际上第一行的内容。我已经检查过,给_items.Add()的项目是一个有效的对象。我现在一直试图解决这个问题一两天,真的需要一些帮助。提前谢谢。

编辑:

ListingsController.xaml.cs

using CSGOItemManager.BitSkins;
using System;
using System.Collections.ObjectModel;
using System.Threading;
using System.Timers;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows;
using System.Windows.Threading;
using System.Collections.Specialized;

namespace CSGOItemManager.Views
{
    /// <summary>
    /// Interaction logic for BitSkinsListingsController.xaml
    /// </summary>
    public partial class BitSkinsListingsController : UserControl
    {
        private BitSkinsListingsViewModel _viewModel = new BitSkinsListingsViewModel();

        public BitSkinsListingsController()
        {
            InitializeComponent();

            DataContext = _viewModel;

            ListView lvListings = new ListView();
            AddChild(lvListings);

            lvListings.ItemsSource = _viewModel.Items;

            GridView grid = new GridView();

            GridViewColumn itemColumn = new GridViewColumn();
            itemColumn.Header = "Item";
            Binding nameBinding = new Binding("Name");
            nameBinding.Source = _viewModel.Items;
            itemColumn.DisplayMemberBinding = nameBinding;
            itemColumn.Width = 100;

            grid.Columns.Add(itemColumn);

            lvListings.View = grid;
        }
    }

    class BitSkinsListingsViewModel : ModelBase
    {
        private ObservableCollection<ItemModel> _items = new ObservableCollection<ItemModel>();

        public ObservableCollection<ItemModel> Items
        {
            get { return _items; }
        }

        private readonly SynchronizationContext _syncContext;

        public BitSkinsListingsViewModel()
        {
            int i;

            for(i = 0; i < 20; i++)
               _items.Add(new ItemModel() { Name = string.Format("Item {0}", i) });

            _syncContext = SynchronizationContext.Current;

            System.Timers.Timer mTimer = new System.Timers.Timer(1000);
            mTimer.Elapsed += new ElapsedEventHandler(updateItemsCallback);
            mTimer.Enabled = true;
        }

        public void addNewItemsToList(ItemModel item)
        {
            Console.WriteLine(item.Name);
            try
            {
                _items.Add(item);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private void updateItemsCallback(object source, ElapsedEventArgs e)
        {
            try
            {
                Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new ThreadStart(delegate
                {
                    ItemModel newItem = new ItemModel();
                    newItem.Name = "Update Call";
                    //                    _items.Add(newItem);
                    //_syncContext.Post(o => addNewItemsToList(newItem), null);
                    addNewItemsToList(newItem);
                }));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }

    class ItemModel : ModelBase
    {
        private long _ItemID;

        public long ItemID
        {
            get { return _ItemID; }
            set { _ItemID = value; RaisePropertyChangedEvent("ItemID"); }
        }

        private string _Name;

        public string Name
        {
            get { return _Name; }
            set { _Name = value; RaisePropertyChangedEvent("Name"); }
        }
        public string Condition;
        public string Rarity;

        public decimal Float;
        public decimal PatternID;

        public decimal Price;
        public decimal MarketPrice;

        public string InspectURL;
        public string Image;

        public Sticker[] Stickers = new Sticker[4];

        public class Sticker
        {
            public string Name { get; set; }
            public string Image;
            public decimal Wear;
        }

        public static explicit operator ItemModel(Item v)
        {
            ItemModel thisItem = new ItemModel();

            thisItem.ItemID = v.ItemID;

            thisItem.Name = v.Name;
            thisItem.Condition = v.Condition;
            thisItem.Rarity = v.Rarity;

            thisItem.Float = v.Float;
            thisItem.PatternID = v.PatternID;

            thisItem.Price = v.Price;
            thisItem.MarketPrice = v.MarketPrice;

            thisItem.InspectURL = v.InspectURL;
            thisItem.Image = v.Image;

            return thisItem;
        }
    }
}

ListingsController.xaml

<UserControl x:Class="CSGOItemManager.Views.BitSkinsListingsController"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:CSGOItemManager.Views"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
</UserControl>

0 个答案:

没有答案