[英]Dependancy Property with Observable Collection
public IEnumerable<Location> Locations
{
get { return (IEnumerable<Location>)GetValue(LocationsProperty); }
set { SetValue(LocationsProperty, value); }
}
public static readonly DependencyProperty LocationsProperty =
DependencyProperty.Register("Locations", typeof(IEnumerable<Location>), typeof(userControl), new PropertyMetadata(null));
public class Location
{
public db.Location DbLocation { get; set; }
public ObservableCollection<db.Person> DbContactsInLocation { get; set; }
}
Locations = myDataContext.Locations
.Where(l => l.Active == "Y")
.OrderBy(l => l.Name)
.Select(l => new Location {
DbLocation = l
**DbContactsInLocation = l.PersonLocations.Select(pl => pl.personLocation.person)**
}).ToList();
I'm getting: 我越来越:
Cannot implicitly convert type System.Collections.Generic.IEnumerable
<myDataContext.Person>'
to 'System.Collections.ObjectModel.ObservableCollection<myDataContext.Person>'
An explicit conversion exists (are you missing a cast?) 无法将类型System.Collections.Generic.IEnumerable<myDataContext.Person>'
隐式转换为'System.Collections.ObjectModel.ObservableCollection<myDataContext.Person>'
存在显式转换(您是否错过了转换?)
Any ideas? 有任何想法吗?
You need to insert your LINQ query into an ObservableCollection<T>(IEnumerable<T>)
constructor. 您需要将LINQ查询插入到ObservableCollection<T>(IEnumerable<T>)
构造函数中。 Try this: 尝试这个:
DBContactsInLocation = new ObservableCollection<myDataContext.Person>(l.PersonLocations.Select(pl => pl.personLocation.person).ToList());
Wrap your linq query in an ObservableCollection<T>(IEnumerabele<T>)
constructor . 将您的linq查询包装在ObservableCollection<T>(IEnumerabele<T>)
构造函数中 。
DbContactsInLocation = new ObservableCollection<db.Person>(
l.PersonLocations.Select(pl => pl.personLocation.person)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.