Silverlight Observable Collection problem
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'
开发者_如何学CAny idea why this is happening? and how to get around it?
Thanks for your time...
Change your 'set' from 'copy by reference' to 'copy by value'.
set { _employeesToDisplay = new ObservableCollection<Employee>( value ); }
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
精彩评论