All of my data in my Observable collection is the same, how do I fix this?

london_jblondon_jb Member ✭✭

Hey,

I have a binding to an ObservableCollection that keeps overwriting all the data in the collection with whatever is current when I attempt to add to the collection or whenever the current data itself is changed. I know there is a simple fix for this but I'm not sure what I am missing.

Answers

  • LeonLuLeonLu Member, Xamarin Team Xamurai
    edited June 17

    If you add the data to the ObservableCollection or current data is changed, all of the data will be changed to same? If so, it is weird, I wrote a demo to you about this scenario, you can refer to it.

    There is MainPage.xaml

          <StackLayout>
        <!-- Place new controls here -->
        <Button x:Name="btnAdd" Text="add data"/>
        <Button x:Name="btnChange" Text="change data"/>
    
        <ListView x:Name="mylistview" >
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextCell Text="{Binding Name, Mode=TwoWay }"  Detail="{Binding Age ,Mode=TwoWay}"/>
    
                </DataTemplate>
            </ListView.ItemTemplate>
    
        </ListView>
    </StackLayout>
    

    Here is MainPage.xaml.cs

         public partial class MainPage : ContentPage
       {
        ObservableCollection<Person> Persons = new ObservableCollection<Person>();
        Person personTest;
       // public ObservableCollection<Person> Persons { get { return employees; } }
        public MainPage()
        {
            InitializeComponent();
    
            personTest = new Person() { Name = "Rob Finnerty", Age = 12 };
            Persons.Add(personTest);
            Persons.Add(new Person() { Name = "Bill Wrestler", Age = 12 });
            Persons.Add(new Person() { Name = "Dr. Geri-Beth Hooper", Age = 12 });
            Persons.Add(new Person() { Name = "Dr. Keith Joyce-Purdy", Age = 12 });
            mylistview.ItemsSource = Persons;
            btnAdd.Clicked += BtnAdd_Clicked;
            btnChange.Clicked += BtnChange_Clicked;
        }
    
        private void BtnChange_Clicked(object sender, EventArgs e)
        {
            personTest.Name = "leon Lu";
        }
    
        private void BtnAdd_Clicked(object sender, EventArgs e)
        {
            Persons.Add(new Person() { Name = "Sheri Spruce", Age = 12 });
        }
        }
    

    There is Person.cs

         public class Person: INotifyPropertyChanged
       {
        public event PropertyChangedEventHandler PropertyChanged;
        string name;
        public string Name {
    
            set
            {
                if (name != value)
                {
                    name = value;
                    OnPropertyChanged("Name");
                }
            }
            get
            {
                return name;
            }
        }
        public int Age { get; set; }
    
    
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
      }
    

    You could upload your code or demo here as well.

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    Are there any updates for this issue? If the reply is helpful, please try to mark it as an answer, it will help others who have similar issue.

  • cbdcbd Member ✭✭

    How are you creating the data for your ObservableCollection?
    Is each object added to the source a new class instance?
    Easy to miss out the "new" creation if you're tired or in a rush.

  • london_jblondon_jb Member ✭✭

    I was trying to add my code but it wouldn't let me. Said it was waiting for approval

  • london_jblondon_jb Member ✭✭

    @LeonLu said:
    Are there any updates for this issue? If the reply is helpful, please try to mark it as an answer, it will help others who have similar issue.

    So basically I add my View to the collection. Afterward, that item in the collection is still maintaining whatever data I place into that field.

    here's a small snippet:

     // Add a new preset option
            App.SettingsData.SettingsCollection.Add(boxingVM);
    
            boxingVM = new BoxingVM();
    
  • london_jblondon_jb Member ✭✭

    @cbd said:
    How are you creating the data for your ObservableCollection?
    Is each object added to the source a new class instance?
    Easy to miss out the "new" creation if you're tired or in a rush.

    I am adding the new keyword, as you can see above. I then need to default to a new collection afterward.

Sign In or Register to comment.