当我尝试更新ListView中插入元素的值时,我没有看到手机上的更改,但是在调试消息中我看到了更改。这是我的代码。
private void chat_LayoutUpdated(object sender, object e)
{
foreach (Message item in chat.Items)
{
item.MessageTime = GetRelativeDate(item.MessageDateTime);
Debug.WriteLine(item.MessageTime); //Display changed value(Delta computed on step before) but on phone screen value didn't change;
}
}
GetRelativeDate //长函数,在发出消息时返回当前时间和时间之间的差值。
class Message // Model of chat message
{
public string MessageText { get; set; }
public string MessageTime { get; set; } // This value i want to change in ListView.
public DateTime MessageDateTime { get; set; }
}
XAML
<TextBlock Grid.Column="0"
FontSize="22"
Text="{Binding MessageText}" />
<TextBlock
Grid.Column="1"
Grid.Row="1"
FontSize="10"
Text="{Binding MessageTime}" />
P / s也许我需要更具体的东西用作聊天窗口。
无论如何,谢谢你们,我真的很感激任何答案或建议。
答案 0 :(得分:3)
需要实施INofityPropertyChanged
MSDN: inotifypropertychanged Example
MSDN文章中的示例:
public class DemoCustomer : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
// This method is called by the Set accessor of each property.
// The CallerMemberName attribute that is applied to the optional propertyName
// parameter causes the property name of the caller to be substituted as an argument.
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private DemoCustomer()
{
}
private string customerNameValue = String.Empty;
public string CustomerName
{
get
{
return this.customerNameValue;
}
set
{
if (value != this.customerNameValue)
{
this.customerNameValue = value;
NotifyPropertyChanged();
}
}
}
}
答案 1 :(得分:0)
谢谢@ChubosaurusSoftware! 。
这里我如何实现该接口可能是一些需要的例子,或者我不太清楚,你可以建议更好的解决方案。
首先添加到使用System.ComponentModel; 然后改变那样的模型。
class Message : INotifyPropertyChanged
{
public string MessageText { get; set; }
private string messageTime = String.Empty;
public string MessageTime
{
get { return this.messageTime; }
set
{
if (value != this.messageTime)
{
this.messageTime = value;
NotifyPropertyChanged("MessageTime");
}
}
}
public DateTime MessageDateTime { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
答案 2 :(得分:0)
我尝试了这个,它对我有用
List<Model.BeneficiaryItem> oldListItem = listItem;
List<Model.DataType> newListItem = new List<Model.DataType>();
ListView.ItemsSource = newListItem;
//update the data
for (int i = 0; i < oldListItem.Count; i++)
{
Model.DataTyep benItem = new Model.DataTyep ();
// update the individual object benItem
newListItem.Add(benItem);
}
ListViewBenList.ItemsSource = newListItem;