[英]Update ListView After Assigning ItemSource to ListView
我對這個話題很困惑,我只是在用一個Single ListView。
<ListView.ItemTemplate >
<DataTemplate >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Image Source="Assets/button_register.png" Stretch="None" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="8,15,8,8" />
<TextBlock Text="{Binding Sender}" FontSize="18" Foreground="Black" FontWeight="Bold" HorizontalAlignment="Center" Margin="0,12,0,0"/>
<Image Source="Assets/button_register.png" Grid.Row="1" Stretch="None" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="4,0" />
<TextBlock Text="{Binding Receiver}" FontSize="18" Foreground="Black" FontWeight="Bold" HorizontalAlignment="Center" Grid.Row="1" Margin="0,12,0,0" VerticalAlignment="Top"/>
<Image Source="Assets/scroll_line_addcategory.png" Grid.Row="2" Stretch="None" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="4,8,4,0" />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
並且此列表視圖與以下類綁定
public class User : INotifyPropertyChanged
{
public string Sender = string.Empty;
public string Receiver = string.Empty;
public string SenderVisibility = string.Empty;
public string ReceiverVisibility = string.Empty;
public event PropertyChangedEventHandler PropertyChanged;
public string send
{
get
{
return this.Sender;
}
set
{
if (value != this.Sender)
{
this.Sender = value;
NotifyPropertyChanged("send");
}
}
}
public string receive
{
get
{
return this.Receiver;
}
set
{
if (value != this.Receiver)
{
this.Receiver = value;
NotifyPropertyChanged("receive");
}
}
}
public string receivevisible
{
get
{
return this.ReceiverVisibility;
}
set
{
if (value != this.ReceiverVisibility)
{
this.ReceiverVisibility = value;
NotifyPropertyChanged("receivevisible");
}
}
}
public string sendervisibility
{
get
{
return this.SenderVisibility;
}
set
{
if (value != this.SenderVisibility)
{
this.SenderVisibility = value;
NotifyPropertyChanged("sendervisibility");
}
}
}
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
在那時加載應用程序時,我只是在6時加載了靜態數據。
private void Page_Loaded(object sender, RoutedEventArgs e)
{
//List<User> ls = new List<User>();
for (int a = 0; a <= 5; a++)
{
User u = new User();
u.Sender = "Hello";
u.Receiver = "World";
ls.Add(u);
}
lst1.ItemsSource = ls;
Debug.WriteLine("Ls Count :: " + ls.Count);
}
但是問題再次出現,我在同一源列表中添加數據。分配源后,但源列表不是更新。
private void btnSend_Click(object sender, RoutedEventArgs e)
{
User n=new User(){
Sender="Chirag",
Receiver="Solanki"
};
ls.Add(n);
lst1.ItemsSource = ls;
}
因此,Plz如果有人有任何出色的想法,請幫助我。
只需設置ItemsSource
就足夠了。 再次設置不會改變任何內容。 代替List<User>
您應該使用ObservableCollection<User>
。 它將自動處理列表中的更改。
此外,您在User
類中有很多錯誤。 這是固定代碼:
class User : INotifyPropertyChanged
{
private string sender = string.Empty;
private string receiver = string.Empty;
private string senderVisibility = string.Empty;
private string receiverVisibility = string.Empty;
public string Sender
{
get
{
return sender;
}
set
{
if (value != this.sender)
{
this.sender = value;
NotifyPropertyChanged("Sender");
}
}
}
public string Receiver
{
get
{
return this.receiver;
}
set
{
if (value != this.receiver)
{
this.receiver = value;
NotifyPropertyChanged("Receiver");
}
}
}
public string ReceiverVisibility
{
get
{
return this.receiverVisibility;
}
set
{
if (value != this.receiverVisibility)
{
this.receiverVisibility = value;
NotifyPropertyChanged("ReceiverVisibility");
}
}
}
public string SenderVisibility
{
get
{
return this.senderVisibility;
}
set
{
if (value != this.senderVisibility)
{
this.senderVisibility = value;
NotifyPropertyChanged("SenderVisibility");
}
}
}
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
調用NotifyPropertyChanged(propertyName)
, propertyName
的值必須與propertyName
的名稱匹配。 您搞砸了屬性和字段。 這些私有的小寫字母被歸檔。 駱駝公案的值是屬性。 在XAML中,您必須綁定屬性而不是字段。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.