Problem binding a ListView for UWP (blank field..)

Hi,

I am working on a POC and one of the more fundamental tests is about getting a simple ListView to data bind properly to an ObservableCollection by testing out the Add, Delete, Replace for an individual item via a button click. I lack access to a Mac and thus haven't been able to run on iOS, but on Android it all seems to work fine, but on UWP a replaced item field becomes blank which as vey annoying... (Add/Delete works fine)...
Code for the view model class and MainPage as below.

Any suggestions?
Many thanks in advance!

using System.ComponentModel;   
 public class TestItem : INotifyPropertyChanged
        {
            private string header;
            public string Header
            {
                get { return header; }
                set
                {
                    header = value;
                    OnPropertyChanged("Header");
                }
            }
            private string info1;
            public string Info1
            {
                get { return info1; }
                set
                {
                    info1 = value;
                    OnPropertyChanged("Info1");
                }
            }

      private string info2;
            public string Info2
            {
                get { return info2; }
                set
                {
                    info2 = value;
                    OnPropertyChanged("Info2");
                }
            }

            public event PropertyChangedEventHandler PropertyChanged;
            public void OnPropertyChanged(string name)
            {
                //Debug.WriteLine("OnPropertyChanged -> " + name);
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null)
                    handler(this, new PropertyChangedEventArgs(name));
            }
        }


    public partial class MainPage : ContentPage
        {
            App app = Application.Current as App;

            // Observable list
            ObservableCollection<TestItem> MyItems;

            int i = 0;

            public MainPage()
            {
                InitializeComponent();

                // Create a new list and add som initial info
                MyItems = new ObservableCollection<TestItem>();
                MyItems.Add(new TestItem() { Header = "Company A", Info1 = "Jane", Info2 = "Thomas" });
                MyItems.Add(new TestItem() { Header = "Company B", Info1 = "Martin", Info2 = "Peter" });

                // Set the databinding source
                myListView.ItemsSource = MyItems;
            }

            void OnButtonClicked(object sender, EventArgs args)
            {
                // test to update items
                i++;
                MyItems[0].Info1 = "Scilly name " + i;
                MyItems[1].Info2 = "Scilly name " + i;
            }

        }

    ?xml version="1.0" encoding="utf-8" ?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:local="clr-namespace:App7"
                 xmlns:sys="clr-namespace:System;assembly=mscorlib"
                 x:Class="App7.MainPage">

      <StackLayout VerticalOptions="Center">
        <Grid>
          <Grid.RowDefinitions>
            <RowDefinition Height="100"/>
            <RowDefinition Height="*"/>
          </Grid.RowDefinitions>

          <Button Grid.Row="0"
                     Text="UpdateButton"
                     Clicked="OnButtonClicked"
                     HorizontalOptions="Center"/>

          <ListView Grid.Row="1"
                    x:Name="myListView"
                    HorizontalOptions="Center"
                    SeparatorVisibility="Default">

            <ListView.ItemTemplate>
              <DataTemplate>
                <ViewCell>
                  <ContentView Padding="10">
                    <Frame OutlineColor="Accent" Padding="10">
                      <Grid>
                        <Grid.RowDefinitions>
                          <RowDefinition Height="Auto"/>
                          <RowDefinition Height="Auto"/>
                          <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <Label  Grid.Row="0"  Text="{Binding Header}" />
                        <Label Grid.Row="1"  Text="{Binding Info1}"  />
                        <Label Grid.Row="2"  Text="{Binding Info2}" />
                      </Grid>
                    </Frame>
                  </ContentView>
                </ViewCell>
              </DataTemplate>
            </ListView.ItemTemplate>
          </ListView>

        </Grid>
      </StackLayout>
    </ContentPage>

Posts

Sign In or Register to comment.