A Microsoft framework for building cross-platform mobile apps using .NET and C# with native performance and user interfaces.
Hello,
Welcome to our Microsoft Q&A platform!
You could try to add event SelectionChanged in your CollectionView.
Please refer to the following code:
<CollectionView ItemsSource="{Binding Monkeys}"
x:Name="myCollectionView"
SelectedItem="{Binding SelectedMonkey,Mode=TwoWay}"
SelectionChanged="CollectionView_SelectionChanged"
SelectionMode="Single">
<!-- other code-->
</CollectionView >
and add function CollectionView_SelectionChanged in your page:
private void CollectionView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var view = sender as CollectionView;
if (view.SelectedItem != null)
{
UpdateSelectionData(e.PreviousSelection, e.CurrentSelection);
}
}
void UpdateSelectionData(IEnumerable<object> previousSelectedItems, IEnumerable<object> currentSelectedItems)
{
Monkey model = (currentSelectedItems.FirstOrDefault() as Monkey);
Navigation.PushAsync(new PokeDetailPage(model));
myCollectionView.SelectedItem = null;
}
Update:
You can just set the binded model of property SelectedItem to nulll.
I made a test on my side , and it works properly. You can refer to the following code:
<CollectionView ItemsSource="{Binding Monkeys}"
x:Name="myCollectionView"
SelectedItem="{Binding SelectedMonkey,Mode=TwoWay}"
SelectionChangedCommand="{Binding SelectedItemCommand}"
SelectionMode="Single">
</CollectionView>
The ViewModel is MonkeysViewModel
public class MonkeysViewModel : INotifyPropertyChanged
{
readonly IList<Monkey> source;
Monkey selectedMonkey;
public ObservableCollection<Monkey> Monkeys { get; private set; }
public Monkey SelectedMonkey
{
get
{
return selectedMonkey;
}
set
{
if (selectedMonkey != value)
{
selectedMonkey = value;
OnPropertyChanged();
}
}
}
public ICommand SelectedItemCommand { get; set; }
public MonkeysViewModel()
{
source = new List<Monkey>();
CreateMonkeyCollection();
selectedMonkey = Monkeys.Skip(3).FirstOrDefault();
SelectedItemCommand = new Command(async () =>
{
if (SelectedMonkey!=null) {
var nav = Application.Current.MainPage;
await nav.Navigation.PushAsync(new PokeDetailPage(SelectedMonkey));
SelectedMonkey = null;
}
});
}
void CreateMonkeyCollection()
{
source.Add(new Monkey
{
Name = "Baboon",
Location = "Africa & Asia",
Details = "Baboons are African and Arabian Old World monkeys belonging to the genus Papio, part of the subfamily Cercopithecinae.",
ImageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/f/fc/Papio_anubis_%28Serengeti%2C_2009%29.jpg/200px-Papio_anubis_%28Serengeti%2C_2009%29.jpg"
});
source.Add(new Monkey
{
Name = "Capuchin Monkey",
Location = "Central & South America",
Details = "The capuchin monkeys are New World monkeys of the subfamily Cebinae. Prior to 2011, the subfamily contained only a single genus, Cebus.",
ImageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/4/40/Capuchin_Costa_Rica.jpg/200px-Capuchin_Costa_Rica.jpg"
});
source.Add(new Monkey
{
Name = "Blue Monkey",
Location = "Central and East Africa",
Details = "The blue monkey or diademed monkey is a species of Old World monkey native to Central and East Africa, ranging from the upper Congo River basin east to the East African Rift and south to northern Angola and Zambia",
ImageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/8/83/BlueMonkey.jpg/220px-BlueMonkey.jpg"
});
Monkeys = new ObservableCollection<Monkey>(source);
}
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
Best Regards,
Jessie Zhang
---
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.