Silverlight可观察集合问题
我有两个如下所示的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将“设置”从“按引用复制”更改为“按值复制”。
Change your 'set' from 'copy by reference' to 'copy by value'.
您能否提供更多代码?这两个集合彼此独立,尽管它们指向相同的对象,但它们并不相互依赖,因此清除一个集合不应修改另一个集合。
此外,您不能使用 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