Skip to content

Instantly share code, notes, and snippets.

@mattregul mattregul/TestPage.xaml Secret
Created Feb 17, 2016

Embed
What would you like to do?
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="ListviewSelectTest.TestPage">
<StackLayout x:Name="myStackLayout" Padding="0, 20, 0, 0">
<Label Text="Clears selected when previous record is selected several times in a row (2-3 clicks)" BackgroundColor="Black" TextColor="#ADE9D5" />
<ListView x:Name="ListClearOnOldRecord" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" CachingStrategy="RecycleElement" ItemsSource="{Binding People}" ItemSelected="OnItemSelected_ClearOnOldRecord" ItemTapped="OnItemTapped_ClearOnOldRecord">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout x:Name="TemplateStack" Orientation="Horizontal" Spacing="10" HorizontalOptions="FillAndExpand" VerticalOptions="CenterAndExpand">
<Label x:Name="FirstName" Text="{Binding FirstName}" />
<Label x:Name="LastName" Text="{Binding LastName}" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Label Text="Clears when any record is selected" BackgroundColor="Black" TextColor="#ADD8E6" />
<ListView x:Name="ListClearOnEveryRecord" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" CachingStrategy="RecycleElement" ItemsSource="{Binding People}" ItemSelected="OnItemSelected_ClearOnEveryRecord" ItemTapped="OnItemTapped_ClearOnEveryRecord">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout x:Name="TemplateStack" Orientation="Horizontal" Spacing="10" HorizontalOptions="FillAndExpand" VerticalOptions="CenterAndExpand">
<Label x:Name="FirstName" Text="{Binding FirstName}" />
<Label x:Name="LastName" Text="{Binding LastName}" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage>
using Xamarin.Forms;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Threading.Tasks;
namespace ListviewSelectTest
{
public partial class TestPage : ContentPage
{
public ObservableCollection<Person> People { get; set; }
private Person lastPerson;
public TestPage ()
{
People = new ObservableCollection<Person> ();
People.Add (
new Person {
FirstName = "Peter",
LastName = "C",
});
People.Add (
new Person {
FirstName = "Tricia",
LastName = "C",
}
);
People.Add (
new Person {
FirstName = "Vicki",
LastName = "Y",
}
);
People.Add (
new Person {
FirstName = "Chris",
LastName = "R",
}
);
People.Add (
new Person {
FirstName = "Alan",
LastName = "G",
}
);
InitializeComponent ();
myStackLayout.BindingContext = this;
}
protected void OnItemTapped_ClearOnOldRecord (object sender, ItemTappedEventArgs e)
{
Debug.WriteLine ("OnItemTapped_ClearOnOldRecord()");
if (e.Item == lastPerson) {
Debug.WriteLine ("Tapped Same Row - Try to Clear Selection");
// This appears to only work if you click several times
ListClearOnOldRecord.SelectedItem = null;
} else if (e.Item != lastPerson) {
Debug.WriteLine ("Tapped New Row");
lastPerson = (Person)e.Item;
}
}
protected void OnItemSelected_ClearOnOldRecord (object sender, SelectedItemChangedEventArgs e)
{
//if (e.SelectedItem == null) {
// Debug.WriteLine ("OnItemSelected_ClearOnOldRecord() :: e.SelectedItem == null (Called from Deselection)");
// return; //ItemSelected is called on deselection, which results in SelectedItem being set to null
//} else {
// Debug.WriteLine ("OnItemSelected()");
//}
}
protected void OnItemTapped_ClearOnEveryRecord (object sender, ItemTappedEventArgs e)
{
Debug.WriteLine ("OnItemTapped_ClearOnEveryRecord()");
if (e.Item == lastPerson) {
Debug.WriteLine ("Tapped Same Row");
} else if (e.Item != lastPerson) {
Debug.WriteLine ("Tapped New Row");
lastPerson = (Person)e.Item;
}
// Setting SelectedItem = null here will deselect the listitem everytime, regardless of new record or not
ListClearOnEveryRecord.SelectedItem = null;
}
protected void OnItemSelected_ClearOnEveryRecord (object sender, SelectedItemChangedEventArgs e)
{
//if (e.SelectedItem == null) {
// Debug.WriteLine ("OnItemSelected_ClearOnEveryRecord() :: e.SelectedItem == null (Called from Deselection)");
// return; //ItemSelected is called on deselection, which results in SelectedItem being set to null
//} else {
// Debug.WriteLine ("OnItemSelected()");
//}
}
}
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
}
Xamarin Forms - 2.0.1.6505
===============
=== Xamarin Studio ===
Version 5.10.2 (build 56)
Installation UUID: 3477b147-e3de-47d4-87f5-4f0230db8f1e
Runtime:
Mono 4.2.2 (explicit/996df3c)
GTK+ 2.24.23 (Raleigh theme)
Package version: 402020030
=== Xamarin.Profiler ===
Version: 0.24.0.0
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler
=== Xamarin.Android ===
Version: 6.0.1.10 (Enterprise Edition)
Android SDK: /Users/matthewr/Library/Developer/Xamarin/android-sdk-macosx
Supported Android versions:
2.3 (API level 10)
4.0.3 (API level 15)
4.4 (API level 19)
5.0 (API level 21)
5.1 (API level 22)
6.0 (API level 23)
SDK Tools Version: 24.4.1
SDK Platform Tools Version: 23.0.1
SDK Build Tools Version: 22.0.1
Java SDK: /usr
java version "1.7.0_71"
Java(TM) SE Runtime Environment (build 1.7.0_71-b14)
Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)
=== Xamarin Android Player ===
Version: 0.6.5
Location: /Applications/Xamarin Android Player.app
=== Xamarin Inspector ===
Version: 0.5.0.0
Hash: 45b35bb
Branch: master
Build date: Thu Jan 14 18:53:32 UTC 2016
=== Apple Developer Tools ===
Xcode 7.2.1 (9548.1)
Build 7C1002
=== Xamarin.iOS ===
Version: 9.4.1.25 (Enterprise Edition)
Hash: 962a050
Branch: master
Build date: 2016-01-29 16:59:11-0500
=== Xamarin.Mac ===
Version: 2.4.1.6 (Enterprise Edition)
=== Build Information ===
Release ID: 510020056
Git revision: bb74ff467c62ded42b7b7ac7fdd2edc60f8647b0
Build date: 2016-01-26 16:24:41-05
Xamarin addins: 8b797d7ba24d5abab226c2cf9fda77f666263f1b
Build lane: monodevelop-lion-cycle6-c6sr1
=== Operating System ===
Mac OS X 10.10.5
Darwin mMac.fios-router.home 14.5.0 Darwin Kernel Version 14.5.0
Tue Sep 1 21:23:09 PDT 2015
root:xnu-2782.50.1~1/RELEASE_X86_64 x86_64
@mattregul

This comment has been minimized.

Copy link
Owner Author

mattregul commented Feb 17, 2016

Bug 38851 - [Forms] Unexpected Listview deselection behaviour (XAML)
https://bugzilla.xamarin.com/show_bug.cgi?id=38851

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.