ObservableCollection.SetItem() 由于其保护级别而无法访问

sus*_*oo_ 5 .net c# wpf

我正在通过 WPF 应用程序创建一个 CRUD 应用程序来学习基础知识。但是,当我调用 SetItem() 来更改 ObservableCollection 中的元素时,我遇到了“由于其保护级别而无法访问”的情况。

我尝试过但没有解决问题的事情:

  1. 清洁和重建
  2. 将所有保护级别更改为公共和内部

有没有人有任何可能有帮助的想法或其他建议?提前致谢!

错误 1 ​​'System.Collections.ObjectModel.Collection.SetItem(int, WpfApplication1.User)' 由于其保护级别 C:\Users\sliu\Documents\Visual Studio 2013\Projects\WpfApplication1\WpfApplication1\MainWindow.xaml 而无法访问。 cs 70 27 WpfApplication1

代码失败的地方:

private void onKeyDownHandler(object sender, System.Windows.Input.KeyEventArgs e)
{
    BindingExpression binding = addUser.GetBindingExpression(System.Windows.Controls.TextBox.TextProperty);
    binding.UpdateSource();

    User temp_user;

    if ((e.Key == Key.Return) && !addUser.Text.Equals(""))
    {

        if (modify)
        {
            **//THIS IS WHERE THE PROBLEM IS
            users.SetItem(index, new User() { Name = addUser.Text });**
        }
        else
        {
            users.Add(new User() { Name = addUser.Text });
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

完整代码:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Windows.Forms;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public ObservableCollection<User> users = new ObservableCollection<User>();
        public bool modify = false;
        public int index = -1;

        public MainWindow()
        {
            InitializeComponent();

            users.Add(new User() { Name = "John Doe" });
            users.Add(new User() { Name = "Jane Doe" });

            lbUsers.ItemsSource = users;
        }

        private void btnAddUser_Click(object sender, RoutedEventArgs e)
        {
            users.Add(new User() { Name = "New user" });
        }

        private void btnModify_Click(object sender, RoutedEventArgs e)
        {
            if (lbUsers.SelectedItem != null)
            {
                addUser.Text = (lbUsers.SelectedItem as User).Name;
                modify = true;
                index = users.IndexOf(lbUsers.SelectedItem as User);
            }
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            if (lbUsers.SelectedItem != null)
                users.Remove(lbUsers.SelectedItem as User);
        }

        private void onKeyDownHandler(object sender, System.Windows.Input.KeyEventArgs e)
        {
            BindingExpression binding = addUser.GetBindingExpression(System.Windows.Controls.TextBox.TextProperty);
        binding.UpdateSource();

            User temp_user;

            if ((e.Key == Key.Return) && !addUser.Text.Equals(""))
            {

                if (modify)
                {
                    **//THIS IS WHERE THE PROBLEM IS
                    users.SetItem(index, new User() { Name = addUser.Text });**
                }
                else
                {
                    users.Add(new User() { Name = addUser.Text });
                }
            }
        }
    }

    public class User : INotifyPropertyChanged
    {
        public string name;
        public string Name
        {
            get { return this.name; }
            set
            {
                if (this.name != value)
                {
                    this.name = value;
                    this.NotifyPropertyChanged("Name");
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged(string propName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Yuv*_*kov 4

ObservableCollection<T>.SetItem是一种protected方法,意味着它仅适用于派生类型。

你有两个选择:

  1. 使用Insert(注意这会将元素下移 1,而不是替换最初放置在指定索引中的值):

    users.Insert(0, item);
    
    Run Code Online (Sandbox Code Playgroud)
  2. 使用索引器:

    users[index] = new User { /*....*/ };
    
    Run Code Online (Sandbox Code Playgroud)