Windows phone 7 列表框项目赢得';t使用绑定wp7进行更新
我有一个列表框,带有自定义项。代码:Windows phone 7 列表框项目赢得';t使用绑定wp7进行更新,windows-phone-7,binding,listbox,custom-controls,Windows Phone 7,Binding,Listbox,Custom Controls,我有一个列表框,带有自定义项。代码: <ListBox Height="600" HorizontalAlignment="Left" Margin="7,6,0,0" Name="friendList" VerticalAlignment="Top" Width="449" ItemsSource="{Binding Friends}"> <ListBox.ItemsPanel> <ItemsPanelTem
<ListBox Height="600" HorizontalAlignment="Left" Margin="7,6,0,0" Name="friendList" VerticalAlignment="Top" Width="449" ItemsSource="{Binding Friends}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:WrapPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Margin="5,0">
<Image Height="120" HorizontalAlignment="Left" Name="image" Stretch="Fill" VerticalAlignment="Top" Width="120" Source="{Binding ImageUri}" GotFocus="image_GotFocus"/>
<CheckBox Height="78" HorizontalAlignment="Left" Margin="65,63,0,0" x:Name="selectedChckbox" VerticalAlignment="Top" Width="55" IsChecked="{Binding Selected, Mode=TwoWay}"/>
<TextBlock Height="58" HorizontalAlignment="Left" Margin="0,122,0,0" x:Name="nameTextBlck" VerticalAlignment="Top" Text ="{Binding Title}" Width="120" TextWrapping="Wrap" GotFocus="name_GotFocus"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
ViewModel代码:
public class FacebookFriendSelectionViewModel : INotifyPropertyChanged
{
public FacebookFriendSelectionViewModel()
{
Friends = new ObservableCollection<TempFriends>();
}
/// <summary>
/// A collection for MenuItemViewModel objects.
/// </summary>
public ObservableCollection<TempFriends> Friends { get; private set; }
public void AddItem(TempFriends item)
{
Friends.Add(item);
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
但是,如果我将datacontext设置为null,然后按如下方式分配viewmodel aggain,则列表框将更新值的唯一方法是:
_selectFriendContent.DataContext = null;
_selectFriendContent.DataContext = friendSelectionViewModel;
但刷新列表大约需要5-10秒。我知道有更好的解决办法,但我不知道怎么解决
提前谢谢
TempFriends
类没有实现我所看到的INotifyPropertyChanged
。只需添加公共类TempFriends:INotifyPropertyChanged您的“选定”属性是否会在setter上的PropertyChanged(“选定”)上上升?
public string Title { get; set; }
public string ImageUri { get; set; }
public bool Selected {
get
{
return _selected;
}
set
{
_selected = value;
OnPropertyChanged("Selected");
}
}
public string Id { get; set; }
public TempFriends(string title, string imageUir, bool selected, string id)
{
Title = title;
ImageUri = imageUir;
_selected = Selected = selected;
Id = id;
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
_selectFriendContent.DataContext = null;
_selectFriendContent.DataContext = friendSelectionViewModel;