I am building a CRUD application through a WPF application to learn the basics. However, I run "inaccessible due to its level of protection" when I call SetItem () to change the item in the ObservableCollection.
Things I tried but didn't solve the problem:
- Clean and restore
- Change all levels of protection, both public and internal
Does anyone have any ideas or other suggestions that might help? Thanks in advance!
Error 1 'System.Collections.ObjectModel.Collection.SetItem (int, WpfApplication1.User)' is unavailable due to protection level C: \ Users \ sliu \ Documents \ Visual Studio 2013 \ Projects \ WpfApplication1 \ WpfApplication1 \ MainWindow.xaml.cs 70 27 WpfApplication1
In case of code failure:
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)
{
**
users.SetItem(index, new User() { Name = addUser.Text });**
}
else
{
users.Add(new User() { Name = addUser.Text });
}
}
}
Integer 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)
{
**
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));
}
}
}