I'm creating a CRUD application through the WPF Application to learn the basics. However, I am running into a 'inaccessible due to its protection level' when I call SetItem() to change an element within the ObservableCollection.
Things that I have tried but did not solve the problem:
Does anyone have any ideas or other suggestions that may help? Thanks in advance!
Error 1 'System.Collections.ObjectModel.Collection.SetItem(int, WpfApplication1.User)' is inaccessible due to its protection level C:\Users\sliu\Documents\Visual Studio 2013\Projects\WpfApplication1\WpfApplication1\MainWindow.xaml.cs 70 27 WpfApplication1
Where the code is failing:
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 });
        }
    }
}
Entire code:
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));
        }
    }
}
ObservableCollection<T>.SetItem is a protected method, meaning its only available to derived types.
You have two choices:
use Insert (Note this will shift the elements down by 1, not replace the value originally placed in the specified index):
users.Insert(0, item);
Use an indexer:
users[index] = new User { /*....*/ };
Use the indexer instead.
users[index] = new User() { Name = addUser.Text };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With