C# LongListSelector和ObservableCollection抛出InvalidCastException
我试图将我的收藏绑定到LLS,但我得到了一个无效的例外。如果我使用ListBox而不是LLS,那么所有绑定都可以很好地工作。这是我的收藏:C# LongListSelector和ObservableCollection抛出InvalidCastException,c#,silverlight,windows-phone-7,longlistselector,C#,Silverlight,Windows Phone 7,Longlistselector,我试图将我的收藏绑定到LLS,但我得到了一个无效的例外。如果我使用ListBox而不是LLS,那么所有绑定都可以很好地工作。这是我的收藏: public class Friend : INotifyPropertyChanged { private string name; public string Name { get { return name; } set { name = value; OnPropertyChanged("Name"); } } pri
public class Friend : INotifyPropertyChanged
{
private string name;
public string Name { get { return name; } set { name = value; OnPropertyChanged("Name"); } }
private string image;
public string Image { get { return image; } set { image = value; OnPropertyChanged("Image"); } }
private string sourseId;
public string SourseId { get { return sourseId; } set { sourseId = value; OnPropertyChanged("SourseId"); } }
private string online_image;
public string Online_Image { get { return online_image; } set { online_image = value; if (online_image == "1") online_image = @"/icons/appbar.power.png"; OnPropertyChanged("Online_Image"); } }
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string info)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(info));
}
}
public Friend() { }
}
public class FriendList : ObservableCollection<Friend>
{
public FriendList()
: base()
{
}
}
和public-FriendList-FriendList=new-FriendList()代码>就在页面下方。我做错了什么?异常在字符串this.AllFriendsList.ItemsSource=FriendsList上激发代码>
这是我的xaml:
<toolkit:LongListSelector Name="AllFriendsList" ItemsSource="{Binding FriendList}">
<toolkit:LongListSelector.ItemTemplate>
<DataTemplate>
<Grid Height="75" Width="460" Margin="0,10,10,0">
<Line Style="{StaticResource Line1}" ></Line>
<Line Style="{StaticResource Line2}" ></Line>
<TextBlock Margin="75,15,40,0" FontSize="30" Name="Name" Text="{Binding Name}" Tap="GetUserInfo" />
<Image HorizontalAlignment="Left" Width="75" Name="Photo" >
<Image.Source>
<BitmapImage UriSource="{Binding Image}" CreateOptions="BackgroundCreation" />
</Image.Source>
</Image>
<Image HorizontalAlignment="Right" Name="IsOnline" Margin="0,0,0,0" Width="60" Height="60" Source="{Binding Online_Image}" />
</Grid>
</DataTemplate>
</toolkit:LongListSelector.ItemTemplate>
</toolkit:LongListSelector>
UPD:如果我使用示例中的group,并将我的列表分组为
var FriendsGroup = from fr in FriendList
group fr by fr.Online_Image into c
orderby c.Key
select new Group<Friend>(c.Key, c);
AllFriendsList.ItemsSource = FriendsGroup;
var FriendsGroup=来自FriendsList中的fr
按fr.Online\u图像将fr分组到c中
orderby c.Key
选择新组(c键,c);
AllFriendsList.ItemsSource=FriendsGroup;
它很好用。如果我理解正确-LLS正在等待分组的源。您将绑定两次到您的AllFiendsList。第一次通过这里的xaml
<toolkit:LongListSelector Name="AllFriendsList" ItemsSource="{Binding FriendList}">
第二次通过代码。这可能会导致异常。我通过将IsFlat更改为True解决了相同的问题
<toolkit:LongListSelector Name="AllFriendsList" ItemsSource="{Binding FriendList}" IsFlat="True">
我尝试绑定代码、xaml、code和xaml-相同的异常。
<toolkit:LongListSelector Name="AllFriendsList" ItemsSource="{Binding FriendList}" IsFlat="True">