Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 75885

Bug with ListView

$
0
0

If I remove from an ObservableCollection list bound into a ListView the TranslationX property of the deleted item is applied to the next item. For example if I delete the item (2) the TranslationX of the old second item is applied to the new item 2 (which was the 3rd).
My example:

<StackLayout>
        <Button Clicked="removeitem" Text="remove first element"></Button>
        <ListView ItemsSource="{Binding items}" x:Name="listView">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <c:GestureListItem />
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>

Code behind to add some data:

    public partial class MainPage : ContentPage
    {
        public ObservableCollection<string> items { get; set; }
        public MainPage()
        {
            items = new ObservableCollection<string>();
            items.Add("first");
            items.Add("second");
            items.Add("third");
            items.Add("forth");
            items.Add("fifth");
            InitializeComponent();
            listView.BindingContext = this;
        }

        private void removeitem(object sender, EventArgs e)
        {
            items.Remove(items[0]);
        }
    }

In my ViewCell version i added a BackgroundColor binding to show that this property works. The problem effects only TranslationX:

<?xml version="1.0" encoding="UTF-8"?>
<ViewCell xmlns="http://xamarin.com/schemas/2014/forms"
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
      xmlns:swipe="clr-namespace:Xamtools"
      x:Class="Xamtools.GestureListItem">
    <BoxView x:Name="boxview" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" TranslationX="{Binding TransX}" BackgroundColor="{Binding BackgroundColor}" />
</ViewCell>

The code behind:

    public partial class GestureListItem : ViewCell
    {
        private static Random r = new Random();
        public GestureListItem()
        {
            InitializeComponent();
            boxview.BindingContext = new Context
            {
                TransX = r.Next(10, 350),
                BackgroundColor = Color.FromRgb(r.Next(250), r.Next(150), r.Next(250))
            };
        }
    }

    public class Context : INotifyPropertyChanged
    {
        private double _transx = 0;
        private Color _color;

        public event PropertyChangedEventHandler PropertyChanged;

        public double TransX { get { return _transx; } set { if (value != _transx) { _transx = value; OnPropertyChanged("TransX"); } } }
        public Color BackgroundColor { get { return _color; } set { if (value != _color) { _color = value; OnPropertyChanged("BackgroundColor"); } } }

        private void OnPropertyChanged(string v)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(v));
        }
    }

Anyway, the problem occurs also if you don't use databinding and set your translationX via code.
What have I to do?


Viewing all articles
Browse latest Browse all 75885

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>