简体   繁体   English

UWP C# 数据绑定

[英]UWP C# Data binding

I am writing contacts app, where I have Name and Surname of people in the ListView:我正在编写联系人应用程序,其中我在 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)当我有方法时: 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)私有无效 bt1_Click(对象发送者,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.我不知道我该怎么做才能让它发挥作用。

Change the type of listOfPeople to ObservableCollection<People> and set the ItemsSource once :listOfPeople的类型更改为ObservableCollection<People>并设置ItemsSource一次

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 });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM