导航ListCollectionView时更新SelectedItem
本文关键字:SelectedItem 更新 ListCollectionView 导航 | 更新日期: 2023-09-27 18:19:49
我有一个绑定到Listbox的ListCOollectionView。当我点击它时,我可以选择一个项目。现在,我希望能够转到列表框中的上一个和下一个项目,并同时将其选中。
我的列表框
<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
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中导航时,如何更新所选图像?
我认为您只需要在导航上设置SelectedImage
属性的实例。PropertyChangedEvent应触发WPF端的更新。
我不确定,但我想它应该看起来像这样:
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;
}
导航后,应将CurrentItem分配给SelectedImage属性。当然,你需要在分配任务之前评估你的导航方法是否返回true。