Igor Meszaros
Igor Meszaros

Reputation: 2127

Listbox items won't update with binding wp7

I have a listbox, with custom items. Code:

<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>

I've created a veiwmodel for the values for binding, and when I click on one item I want to change the checkbox state like so:

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

The ViewModel Code:

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

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

But the only way the listbox gets the values updated, if I set the datacontext to null and than assign the viewmodel aggain like so:

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

But this takes about 5-10 seconds to refresh the list. I know there is a better solution, I just cant figure out how.

Thanks in advance!

Upvotes: 1

Views: 3053

Answers (1)

EgorBo
EgorBo

Reputation: 6142

TempFriends class doesn't implement INotifyPropertyChanged as far as I see. Just add public class TempFriends : INotifyPropertyChanged

Upvotes: 3

Related Questions