列表框项目不会使用绑定wp7进行更新

时间:2011-10-24 08:45:19

标签: 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>
                <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>

我为绑定值创建了一个veiwmodel,当我点击一个项目时,我想改变复选框状态,如下所示:

friendSelectionViewModel.Friends[_selectFriendContent.friendList.SelectedIndex].Selected = !friendSelectionViewModel.Friends[_selectFriendContent.friendList.SelectedIndex].Selected;

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));
        }
    }
}

} 公共课TempFriends {     bool _selected;

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));
    }
}

但是列表框获取更新值的唯一方法是,如果我将datacontext设置为null并且像这样分配viewmodel aggain:

  _selectFriendContent.DataContext = null;
    _selectFriendContent.DataContext = friendSelectionViewModel;

但这需要大约5-10秒来刷新列表。我知道有一个更好的解决方案,我只是想弄清楚如何。

提前致谢!

1 个答案:

答案 0 :(得分:3)

就我看来,

TempFriends类没有实现INotifyPropertyChanged。只需添加公共类TempFriends: INotifyPropertyChanged