将 ItemSource 分配给 ListView 后更新 ListView
Update ListView After Assigning ItemSource to ListView
我对此感到很困惑topic.I 我只使用了一个 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>
并且此列表视图与以下 class
绑定
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;
}
所以,如果有人有什么好主意,请帮助我。
设置一次ItemsSource
就够了。再次设置它不会改变任何东西。您应该使用 ObservableCollection<User>
而不是 List<User>
。它将自动处理列表中的更改。
此外,您在User
class中有很多错误。这是固定代码:
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
的值必须与 属性 的名称匹配。你搞砸了属性和字段。这些私有的小写字母是归档的。 Public 驼峰式值是属性。在 XAML 中,您必须绑定属性而不是字段。
我对此感到很困惑topic.I 我只使用了一个 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>
并且此列表视图与以下 class
绑定 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;
}
所以,如果有人有什么好主意,请帮助我。
设置一次ItemsSource
就够了。再次设置它不会改变任何东西。您应该使用 ObservableCollection<User>
而不是 List<User>
。它将自动处理列表中的更改。
此外,您在User
class中有很多错误。这是固定代码:
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
的值必须与 属性 的名称匹配。你搞砸了属性和字段。这些私有的小写字母是归档的。 Public 驼峰式值是属性。在 XAML 中,您必须绑定属性而不是字段。