2014-12-03 54 views
2

当我尝试更新ListView中插入元素的值时,我没有看到手机上的更改,但在调试消息中,我看到了更改。在这里我的代码。更新ListView项目值不工作Windows Phone 8.1

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 //龙功能将其sended消息时的当前时间和时间之间返回增量。

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也许我需要更具体的使用作为聊天窗口的东西。

无论如何感谢球员我会很感激任何答案或建议。

回答

3

需要从MSDN文章实现INofityPropertyChanged

MSDN: inotifypropertychanged Example


样品:

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(); 
      } 
     } 
    } 
} 
+0

,并把它设定正确的? – 2014-12-03 02:04:27

+0

@DzumoPro是:) – 2014-12-03 02:04:54

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