简体   繁体   中英

clear ListView item selection Xamarin.Forms

I have listView called "Main_Menu" in Navigation Page. When I click it, it shows other page. When I return to Page with Main_Menu, I want do clear selection.
Please help me. Thanks

PS

((ListView)sender).SelectedItem = null; 

causes crash when it onAppear() or onDisappear();

Here is how to set the SelectedItem with the ItemSelected event handler. Add the eventhandler to your XAML file and then add the event handler to its code behind.

<!-Page.xaml-/>
<ListView x:Name="myList"   
    ItemsSource="{Binding Items}"
    SelectedItem="{Binding SelectedItem}"
    HasUnevenRows="true"
    ItemSelected="OnItemSelected"/>

// in your code behind Page.xaml.cs
void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
{
   if (sender!= null && sender is ListView listview)
   {
       if (e != null)  e.SelectedItem = null;
   }
}

This is a solution:

 if (e.SelectedItem == null)
        {
            return;
        }
((ListView)sender).SelectedItem = null;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM