我在这个话题中非常困惑。我只是采用One 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);
}
但问题出现了,而我又在同一列表中添加数据视图分配源但Listview不是更新。
private void btnSend_Click(object sender, RoutedEventArgs e)
{
User n=new User(){
Sender="Chirag",
Receiver="Solanki"
};
ls.Add(n);
lst1.ItemsSource = ls;
}
所以,Plz如果有任何人有任何好主意,请帮助我。
答案 0 :(得分:1)
足以设置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
的值必须与该媒体资源的名称相匹配。你弄乱了属性和字段。这些私人小写的是文件。公共驼峰案例值是属性。在XAML中,您必须绑定属性而不是字段。