使用绑定时,ListBox不会自动更新条目

时间:2014-06-03 14:23:11

标签: c# wpf data-binding listbox

我在添加新数据时更新列表框时遇到了一些问题。这是我的代码

我的列表框有

 <ListBox Name="EmailList" ItemsSource="{Binding ListBoxData, Mode=TwoWay}"

这是我的计划:

public partial class MainWindow : Window
{

    string hostname = Properties.Settings.Default.pop_host;
    int port = Properties.Settings.Default.pop_port;
    bool useSsl = Properties.Settings.Default.pop_usessl;
    string username = "recent:" + Properties.Settings.Default.username;
    string password = Properties.Settings.Default.password;

        // When this button is pressed the program starts a backgroundworker
        // that begins the download of mails
    private void MenuItem_Click(object sender, RoutedEventArgs e)
    {
        BackgroundWorker getNewMail = new BackgroundWorker();
        getNewMail.DoWork += newEmail;
        getNewMail.RunWorkerAsync();
        getNewMail.RunWorkerCompleted += updateList;
    }

    // The actual function that downloads the mails (Using OpenPOP)
    private void newEmail(object sender, DoWorkEventArgs e)
    {
    List<Message> allEmail = FetchAllMessages(hostname, port, useSsl, username, password);
            ListBoxData = new List<EmailEntry> { };

        foreach (Message singleEmail in allEmail)
        {
                var mailData = new ListBoxDataClass { theMessage = singleEmail, truncate = 40 };
                readyUpListBoxData(mailData);
                ListBoxData.Add(new EmailEntry { from = mailData.displayName, subject = mailData.partOfBody, messageID = singleEmail.Headers.MessageId.ToString() });   
        }
    }

    public class ListBoxDataClass
    {
        public Message theMessage { get; set; }
        public int truncate { get; set; }

        public string partOfBody { get; set; }
        public string displayName { get; set; }
    }

    // A function that does different things with the downloaded data
    public void readyUpListBoxData(ListBoxDataClass data)
    {
        MessagePart theEmailTxt = data.theMessage.FindFirstPlainTextVersion();
        string noLineBreaks = theEmailTxt.GetBodyAsText().ToString().Replace(System.Environment.NewLine, " ");
        data.partOfBody = noLineBreaks.Length <= data.truncate ? noLineBreaks : noLineBreaks.Substring(0, data.truncate) + " ..";
        data.displayName = data.theMessage.Headers.From.DisplayName.ToString();
        if (data.displayName == "")
        {
            data.displayName = data.theMessage.Headers.From.Address.ToString();
        }
        data.displayName += " <" + data.theMessage.Headers.From.Address.ToString() + ">";
    }
}

我相信我应该使用一种名为observable collection的东西或类似的东西?我只是看不出我如何在我的程序中使用它。我希望你们中的一些人可以帮助我使用Observable集合,或者指出我可以使用的其他东西,这就是我需要的东西。

我正在考虑使用像计时器这样的东西来实现它,但我不确定它是不是很好的做法?

2 个答案:

答案 0 :(得分:1)

为ListBoxData创建公共属性,如下所示:

     public partial class MainWindow : Window
     {

         public ObservableCollection<EmailEntry > ListBoxData{get;set;}

         public MainWindow()
         {
             ListBoxData = new ObservableCollection<EmailEntry >();
             InitializeComponents();
         }

        private void newEmail(object sender, DoWorkEventArgs e)
        {
             List<Message> allEmail = FetchAllMessages(hostname, port, useSsl, username, password);


            foreach (Message singleEmail in allEmail)
            {
                var mailData = new ListBoxDataClass { theMessage = singleEmail, truncate = 40 };
                readyUpListBoxData(mailData);
                ListBoxData.Add(new EmailEntry { from = mailData.displayName, subject = mailData.partOfBody, messageID = singleEmail.Headers.MessageId.ToString() });   
            }
      }

答案 1 :(得分:-2)

您可以实施INotifyPropertyChanged,然后在完成添加后提升ListBoxData属性,或者只需将ListBoxData更改为ObservableCollection,因为它实现了{{1}默认情况下。