Jolof
Jolof

Reputation: 41

UWP C# Data binding

I am writing contacts app, where I have Name and Surname of people in the ListView:

<ListView x:Name="peopleListView" x:FieldModifier="public">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Height="64">
<Ellipse Height="48" Width="48" VerticalAlignment="Center">
<Ellipse.Fill>
<ImageBrush ImageSource="Assets\StoreLogo.png"/>
</Ellipse.Fill>
</Ellipse>
<StackPanel Orientation="Vertical" VerticalAlignment="Center" Margin="12,0,0,0">
<TextBlock Text="{Binding Path=Name}"  Style="{ThemeResource BaseTextBlockStyle}" Foreground="{ThemeResource SystemControlPageTextBaseHighBrush}"/>
<TextBlock Text="{Binding Path=Surname}" Style="{ThemeResource BodyTextBlockStyle}" Foreground="{ThemeResource SystemControlPageTextBaseMediumBrush}"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>

And there will be more later i details. But I stuck on this problem. I have my class:

public class People : INotifyPropertyChanged
{
private string _name;
private string _surname;
private string _city;

public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string
propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public string Name
{
get { return _name; }
set
{
_name = value;
OnPropertyChanged("Name");
}
}
public string Surname
{
get { return _surname; }
set
{
_surname = value;
OnPropertyChanged("Surname");
}
}
public string City
{
get { return _city; }
set
{
_city = value;
OnPropertyChanged("City");
}
}    
}

When I have method liek this: public void addNew(string name, string surname, string city)

{
listOfPeople.Add(new People() { Name = name, Surname = surname, City = city });
peopleListView.ItemsSource = listOfPeople;
}

And I am adding people from other page with button click :

private void bt1_Click(object sender, RoutedEventArgs e)

{
MainPage.Current.addNew(name.Text.ToString(), name.Text.ToString(), city.Text.ToString());
MainPage.Current.frame.Navigate(typeof(Details));
}

It only adds one and stops there. No matter how many times I am trying to add, there is nothing there, except for the one that I have added first. But when I add them like this:

public MainPage()
{
this.InitializeComponent();
Current = this;
frame.Navigate(typeof(Details));
addNew("Jan", "Kowalski", "Krakow");
addNew("Jan", "Kowalski", "Krakow");
addNew("Jan", "Kowalski", "Krakow");
addNew("Jan", "Kowalski", "Krakow");
}

They are all there, and I can't add another one with that button. And I have no idea what should I do for it to work.

Upvotes: 0

Views: 105

Answers (1)

mm8
mm8

Reputation: 169380

Change the type of listOfPeople to ObservableCollection<People> and set the ItemsSource once:

private readonly ObservableCollection<People> listOfPeople = new ObservableCollection<People>();

public MainPage()
{
    this.InitializeComponent();
    Current = this;
    peopleListView.ItemsSource = listOfPeople;
    frame.Navigate(typeof(Details));
}

public void addNew(string name, string surname, string city)
{
    listOfPeople.Add(new People() { Name = name, Surname = surname, City = city });
}

Upvotes: 1

Related Questions