【问题标题】:Listbox items won't update with binding wp7列表框项目不会通过绑定 wp7 更新
【发布时间】:2011-10-24 08:45:18
【问题描述】:

我有一个带有自定义项目的列表框。代码:

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

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

但是刷新列表大约需要 5-10 秒。我知道有更好的解决方案,但我不知道怎么做。

提前致谢!

【问题讨论】:

  • 您的“Selected”属性是否在 setter 上上升 OnPropertyChanged("Selected")?

标签: windows-phone-7 binding listbox custom-controls


【解决方案1】:

据我所知,TempFriends 类没有实现INotifyPropertyChanged。只需添加公共类 TempFriends :INotifyPropertyChanged

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-27
    • 2012-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多