首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >导航SelectedItem时更新ListCollectionView

导航SelectedItem时更新ListCollectionView
EN

Stack Overflow用户
提问于 2015-10-30 08:18:00
回答 2查看 137关注 0票数 0

我有一个ListCOllectionView,它绑定到一个列表框。当我点击它,我可以选择一个项目。现在,我希望能够转到列表框中的前一项和下一项,并同时选择它。

我的列表箱

代码语言:javascript
复制
<ListBox x:Name="Imported_images" SelectedItem="{Binding SelectedImage}" ItemsSource="{Binding SortedImageLibrary}"/>

<Button x:Name="next" Command="{Binding NextImageCommand}" >
<Button x:Name="previous" Command="{Binding PreviousImageCommand}">

ViewModel

代码语言:javascript
复制
private ListCollectionView _sortedImageLibrary;
public ListCollectionView SortedImageLibrary
{
    get
    {
        if (_sortedImageLibrary == null) 
        {
            _sortedImageLibrary = new ListCollectionView(ImageLibrary);
            _sortedImageLibrary.IsLiveSorting = true;
            _sortedImageLibrary.CustomSort = new ImageComparer();                     
         }
        return _sortedImageLibrary; 
    }
    set
    {
        _sortedImageLibrary = value; RaisePropertyChanged();
    }
}

private Image _selectedImage;
public Image SelectedImage   
{
    get { return _selectedImage; }
    set { _selectedImage = value; RaisePropertyChanged("SelectedImage"); }
}

public RelayCommand NextImageCommand { get; set; }
public RelayCommand PreviousImageCommand { get; set; }
public void PreviousImageExecute()
{
    if (SortedImageLibrary.CurrentPosition == 0)
    {
    }
    else
    {
        SortedImageLibrary.MoveCurrentToPrevious();
    }
}
public void NextImageExecute()
{
    if (SortedImageLibrary.CurrentPosition == SortedImageLibrary.Count - 1)
    {
    }
    else
    {
        SortedImageLibrary.MoveCurrentToNext();
    }
}

我可以转到ListCollectionView中的下一项和前一项,但是SelectedImage不更新。当我在ListCollectionView中导航时,如何更新选定的图像?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-10-30 08:46:37

我认为您只需要在导航上设置SelectedImage属性的实例。PropertyChangedEvent应该在WPF端触发更新。

我不确定,但我想应该像这样:

代码语言:javascript
复制
public void PreviousImageExecute()
{
    if (SortedImageLibrary.CurrentPosition == 0)
    {
    }
    else
    {
        SortedImageLibrary.MoveCurrentToPrevious();
    }
    SelectedImage = SortedImageLibrary.CurrentItem as Image;
}
public void NextImageExecute()
{
    if (SortedImageLibrary.CurrentPosition == SortedImageLibrary.Count - 1)
    {
    }
    else
    {
        SortedImageLibrary.MoveCurrentToNext();
    }
    SelectedImage = SortedImageLibrary.CurrentItem as Image;
}
票数 0
EN

Stack Overflow用户

发布于 2015-10-30 08:49:19

导航之后,应该将CurrentItem分配给SelectedImage属性。当然,您需要评估您的导航方法是否在分配之前返回true。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33431423

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档