【问题标题】:Update ListView items value not working Windows Phone 8.1更新 ListView 项目值不起作用 Windows Phone 8.1
【发布时间】:2014-12-03 01:40:32
【问题描述】:

当我尝试更新 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 也许我需要更具体的东西来用作聊天窗口。

无论如何,谢谢大家,我将非常感谢任何答案或建议。

【问题讨论】:

    标签: c# xaml listview windows-phone windows-phone-8.1


    【解决方案1】:

    需要实现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();
                }
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      感谢@ChubosaurusSoftware! .

      这里我如何实现该接口可能是一些需要的示例,或者我说得不是很清楚,您可以提出更好的解决方案。

      首先添加到 using 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));
              }
          }
      }
      

      【讨论】:

        【解决方案3】:

        我试过了,它对我有用

        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;
        

        【讨论】:

          猜你喜欢
          • 2015-12-20
          • 1970-01-01
          • 2016-11-21
          • 2015-07-07
          • 2015-02-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-03-03
          相关资源
          最近更新 更多