Silverlight可观察集合问题

发布于 2024-10-01 12:41:20 字数 992 浏览 4 评论 0原文

我有两个如下所示的 observalbe 集合,

public ObservableCollection<Employee> AllEmployees
{
  get { return _allEmployees; }
  set { _allEmployees = value;  }
}

public ObservableCollection<Employee> EmployeesToDisplay
{
   get { return _employeesToDisplay; }
   set { _employeesToDisplay = value;  }
}

它们都是由 wcf 服务返回的值单独设置的。

                AllEmployees = ListofEmployees ;
                EmployeesToDisplay= ListofEmployees;

在更改允许我选择活跃员工或非活跃员工的组合框时,我对 'AllEmployees'

varEmployeesEnabled = fromEntity in AllEmployees 运行以下 Linq 查询 其中entity.IsEnabled == true 按实体排序。名称升序 选择实体;

,然后将返回值分配给EmployeesToDisplay,如下所示:

EmployeesToDisplay.Clear();
EmployeesToDisplay.Add(employeesEnabled as Employee);

问题是: 当我经过这行“EmployeesToDisplay.Clear()”时...它甚至清除了“AllEmployees”

知道为什么会发生这种情况吗?以及如何解决它?

感谢您抽出时间...

I have two observalbe collections like below

public ObservableCollection<Employee> AllEmployees
{
  get { return _allEmployees; }
  set { _allEmployees = value;  }
}

public ObservableCollection<Employee> EmployeesToDisplay
{
   get { return _employeesToDisplay; }
   set { _employeesToDisplay = value;  }
}

They are both individually set by the value returned by wcf service.

                AllEmployees = ListofEmployees ;
                EmployeesToDisplay= ListofEmployees;

On change of a combobox that allows me to select Active employees or inactive employees I run the following Linq query on 'AllEmployees'

var employeesEnabled = from entity in AllEmployees
where entity.IsEnabled == true
orderby entity.Name ascending
select entity;

and then assign the returned value to EmployeesToDisplay like below:

EmployeesToDisplay.Clear();
EmployeesToDisplay.Add(employeesEnabled as Employee);

The problem is:
When I go past this line 'EmployeesToDisplay.Clear()' ... it even clears out 'AllEmployees'

Any idea why this is happening? and how to get around it?

Thanks for your time...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾城泪 2024-10-08 12:41:20

将“设置”从“按引用复制”更改为“按值复制”。

set { _employeesToDisplay = new ObservableCollection<Employee>( value );  }

Change your 'set' from 'copy by reference' to 'copy by value'.

set { _employeesToDisplay = new ObservableCollection<Employee>( value );  }
画骨成沙 2024-10-08 12:41:20

您能否提供更多代码?这两个集合彼此独立,尽管它们指向相同的对象,但它们并不相互依赖,因此清除一个集合不应修改另一个集合。

此外,您不能使用 Add 方法将多个项目添加到 ObservableCollection;你必须一次只做一件事情。并且您还尝试将 IEnumerable 转换为 Employee 这将导致将空值添加到您的集合中

Could you provide a bit more code perhaps? The two collections are independent of each other and, although they point to the same objects, they do not depend on each other so clearing one should not modify the other.

Also, you can't add multiple items to the ObservableCollection using the Add method; you must do it one item at a time. And you are also attempting to cast an IEnumerable to Employee which will result in a null value being added to your collection

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文