public IList<string> Items{get;set;}
private string _selectedItem;
public string SelectedItem
{
get=>_selectedItem;
set
{
//if selected item is the same one, return
if(Equals(value,_selectedItem)) return;
//if the item doesn't meet the requirement, don't update the selected item in the view model
if(!ItemMeetsYourRequirement(value))
{
//raise property changed to notify view to change the selected item to previous one
OnPropertyChanged(nameof(SelectedItem));
return;
}
//if you are here, the selected item is changed and meets your requirements;
_selectedItem = value;
OnPropertyChanged(nameof(SelectedItem);
}
}
private bool ItemMeetsYourRequirement(string item) => return true; //add your logic here
3条答案
按热度按时间frebpwbc1#
您可以在
CollectionView_SelectionChanged
事件中deselecte
SelectedItem
:在xaml中:
wpx232ag2#
如果您使用MVVM设计模式,那么您应该能够在
ViewModel
中跟踪CollectionView
的SelectedItem
属性,实际上是在TwoWay
模式下使用Binding。YourView.xaml:
<CollectionView ItemsSource="Items" SelectedItem="SelectedItem"/>
YourViewModel.cs
nlejzf6q3#
https://stackoverflow.com/a/64457513/1894630解决方案的工作算法: