gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 00:51:43 28 4
gpt4 key购买 nike

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

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

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

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

错误 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 });
}
}
}

完整代码:

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 protected方法,这意味着它仅可用于派生类型。

你有两个选择:

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

    users.Insert(0, item);
  2. 使用索引器:

    users[index] = new User { /*....*/ };

关于c# - ObservableCollection.SetItem() 由于其保护级别而无法访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25410320/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com