按下按钮时项目的WPF更新计数

按下按钮时项目的WPF更新计数,wpf,listview,data-binding,Wpf,Listview,Data Binding,新的WPF和编程在这里我试图更新一个项目时,一个按钮被按下,如果该项目已经存在于该ListView,例如,用户输入车和车已经在ListView中的车计数应该从1到2 这是我的InventoryItem类: public class InventoryItem { public string Name { get; set; } public int Count { get; set; } } 这是我的视图模型 public class ViewM

新的WPF和编程在这里我试图更新一个项目时,一个按钮被按下,如果该项目已经存在于该ListView,例如,用户输入车和车已经在ListView中的车计数应该从1到2

这是我的InventoryItem类:

public class InventoryItem
    {
        public string Name { get; set; }
        public int Count { get; set; }
    }
这是我的视图模型

public class ViewModel : ViewModelBase
    {

        private InventoryItem _item;
        private int _count;
        private ObservableCollection<InventoryItem> _inventoryItems;
        private ICommand _addItem;

        public InventoryItem Item
        {
            get 
            {
                return _item;
            }
            set
            {
                _item = value;
                NotifyPropertyChanged("Item");
            }
        }

        public int Count
        {
            get { return _count; }
            set { _count = value; NotifyPropertyChanged("Count"); }
        }

        public ObservableCollection<InventoryItem> InventoryItems
        {
            get
            {
                return _inventoryItems;
            }
            set
            {
                _inventoryItems = value;
                NotifyPropertyChanged("Items");
            }
        }

        public ICommand AddItem
        {
            get
            {
                if (_addItem == null)
                {
                    _addItem = new RelayCommand(ParamArrayAttribute => this.Submit(), null);
                }
                return _addItem;
            }
        }

        public ViewModel()
        {
            Item = new InventoryItem();
            InventoryItems = new ObservableCollection<InventoryItem>();
            InventoryItems.CollectionChanged += new NotifyCollectionChangedEventHandler(InventoryItems_CollectionChanged);
        }

        void InventoryItems_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            NotifyPropertyChanged("Items");
            NotifyPropertyChanged("Count");
        }

        private void Submit()
        {
            if (InventoryItems.Any(p => p.Name == Item.Name))
            {
                InventoryItems.First(x => x.Name == Item.Name).ItemCount++;
            }
            else
            {
                InventoryItems.Add(Item);
            }
        }
    }
public类ViewModel:ViewModelBase
{
私有库存项目_项目;
私人国际单位计数;
私有可观测集合(库存项目);;
私人ICommand _addItem;
公共库存项目
{
得到
{
退货项目;
}
设置
{
_项目=价值;
通知财产变更(“项目”);
}
}
公共整数计数
{
获取{return\u count;}
设置{u count=value;NotifyPropertyChanged(“count”);}
}
公共可观测收集清单项目
{
得到
{
返回库存物品;
}
设置
{
_存货项目=价值;
通知财产变更(“项目”);
}
}
公共ICommand附加项
{
得到
{
如果(_addItem==null)
{
_addItem=new RelayCommand(ParamArrayAttribute=>this.Submit(),null);
}
返回_addItem;
}
}
公共视图模型()
{
Item=新的InventoryItem();
InventoryItems=新的ObservableCollection();
InventoryItems.CollectionChanged+=新的NotifyCollectionChangedEventHandler(InventoryItems\u CollectionChanged);
}
无效库存项\u CollectionChanged(对象发送方,NotifyCollectionChangedEventArgs e)
{
通知财产变更(“项目”);
NotifyPropertyChanged(“计数”);
}
私人提交
{
if(InventoryItems.Any(p=>p.Name==Item.Name))
{
InventoryItems.First(x=>x.Name==Item.Name).ItemCount++;
}
其他的
{
清单项目。添加(项目);
}
}
}
这是我的看法

<ListView x:Name="listBox" ItemsSource="{Binding InventoryItems}" Grid.Row="0" HorizontalAlignment="Stretch" Height="Auto" Margin="10,10,10,60" VerticalAlignment="Stretch">
            <ListView.Resources>
                <Style TargetType="GridViewColumnHeader">
                    <Setter Property="Visibility" Value="Collapsed"/>
                </Style>
            </ListView.Resources>
            <ListView.View>
                <GridView>
                    <GridViewColumn DisplayMemberBinding="{Binding Name}" Width="Auto"/>
                    <GridViewColumn DisplayMemberBinding="{Binding ItemCount}" Width="Auto"/>
                </GridView>
            </ListView.View>
        </ListView>


每当键入列表中存在的项目时,似乎正在更新InventoryItems ObservableCollection,但是没有触发InventoryItems_CollectionChanged事件,因此不会更新ListView。集合是否正在更改,以便触发事件来更新ListView,还是我不理解绑定和事件?

您需要通知InventoryItem类中的属性更改,以更新ListView中ItemCount的更改

快速解决方案:

public class InventoryItem : ViewModelBase
{
    public string Name
    {
        get { return _name; }
        set 
        {
            if (_name != value)
            {
                _name = value;
                NotifyPropertyChanged(nameof(Name));
            }
        }
    }
    private string _name;

    public int ItemCount
    {
        get { return _itemCount; }
        set { _itemCount = value; NotifyPropertyChanged(nameof(ItemCount));
        }
    }
    private int _itemCount;
}
}

ObservableCollection类已包含对INotifyCollectionChanged的处理

对于ObservableCollection,您只需要这一行。您可以删除与ViewModel中的“InventoryItems”和集合有关的所有其他内容

    public ObservableCollection<InventoryItem> InventoryItems { get; } = new ObservableCollection<InventoryItem>();
公共ObservableCollection InventoryItems{get;}=new ObservableCollection(); 另外:向集合中添加项目时,需要创建新项目。否则,您将添加相同的对象,这将不起作用

这是我的简化视图模型,我认为您希望它如何工作:

public class ViewModel : ViewModelBase
{
    private ICommand _addItem;

    public string InputName
    {
        get { return _inputName; }
        set
        {
            if (_inputName != value)
            {
                _inputName = value;
                NotifyPropertyChanged(nameof(InputName));
            }
        }
    }
    private string _inputName;

    public ObservableCollection<InventoryItem> InventoryItems { get; } = new ObservableCollection<InventoryItem>();

    public ICommand AddItem
    {
        get
        {
            if (_addItem == null)
            {
                _addItem = new RelayCommand(ParamArrayAttribute => this.Submit(), null);
            }
            return _addItem;
        }
    }

    private void Submit()
    {
        if (InventoryItems.Any(p => p.Name == InputName))
        {
            InventoryItems.First(x => x.Name == InputName).ItemCount++;
        }
        else
        {
            InventoryItems.Add(new InventoryItem() { Name = InputName, ItemCount = 1 });
        }
    }
}
public类ViewModel:ViewModelBase
{
私人ICommand _addItem;
公共字符串输入名
{
获取{return\u inputName;}
设置
{
if(_inputName!=值)
{
_inputName=值;
NotifyPropertyChanged(nameof(InputName));
}
}
}
私有字符串_inputName;
public ObservableCollection InventoryItems{get;}=new ObservableCollection();
公共ICommand附加项
{
得到
{
如果(_addItem==null)
{
_addItem=new RelayCommand(ParamArrayAttribute=>this.Submit(),null);
}
返回_addItem;
}
}
私人提交
{
if(InventoryItems.Any(p=>p.Name==InputName))
{
InventoryItems.First(x=>x.Name==InputName).ItemCount++;
}
其他的
{
添加(新的InventoryItem(){Name=InputName,ItemCount=1});
}
}
}
为了完成此图,我添加了以下XAML用于测试:

        <TextBox Text="{Binding InputName}" MinWidth="100" Margin="5"/>
        <Button Content="Add" Command="{Binding AddItem}" Margin="5"/>


在notifypropertychanged中使用nameof。这样您就不会意外地为属性更改使用了错误的字符串。就像你现在一样。“Items”不是ViewModelBase的属性名称:请看这个问题。在答案中找到“SetProperty”。这会使你的房子更干净。