UWP C#数据绑定

问题描述 投票:0回答:1

我正在编写联系人应用程序,在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>

[以后还会有更多详细信息。但是我坚持这个问题。我上了课:

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

[当我有方法喜欢时:公共无效addNew(字符串名称,字符串姓氏,字符串城市)

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

并且我通过单击按钮添加其他页面的人员:

private void bt1_Click(对象发送者,RoutedEventArgs e)

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

它仅添加一个并在此处停止。无论我尝试添加多少次,除了我首先添加的内容外,没有任何其他内容。但是当我这样添加它们时:

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

它们都在那里,我无法用该按钮添加另一个。而且我不知道该如何工作。

c# uwp uwp-xaml
1个回答
0
投票

listOfPeople的类型更改为ObservableCollection<People>并设置ItemsSource 一次

© www.soinside.com 2019 - 2024. All rights reserved.