C# ListBox 子項資料更新

來源:互聯網
上載者:User

標籤:方法   name   obj   自動更新   account   des   code   地址   new   

今天在倒騰ListBox控制項的資料編輯時,遇到了一個小小的問題,現在就把解決方案記錄下來,如果各位道友有更好的方法,一定要留言賜教。

 

 

問題還原:

有一個介面,有這麼一個ListBox用來顯示所有的角色資訊:

角色類的原始碼如下,反正可以想象成任意自訂的類都可以:

    /// <summary>    /// 單個角色對象    /// </summary>    public class RoleItem    {        #region Public Property        /// <summary>        /// 角色的唯一代碼        /// </summary>        public string RoleCode { get; set; } = Guid.NewGuid().ToString("N");        /// <summary>        /// 角色名稱        /// </summary>        public string RoleName { get; set; }        /// <summary>        /// 角色描述        /// </summary>        public string Description { get; set; }        /// <summary>        /// 關聯的賬戶列表        /// </summary>        public List<string> Accounts { get; set; } = new List<string>();        #endregion                #region Object Override        /// <summary>        /// 返回對象的字串標識形式        /// </summary>        /// <returns></returns>        public override string ToString()        {            return RoleName;        }                #endregion    }

  在控制項載入的時候進行資料的初始化,這裡不能使用datasouce來綁定資料來源,不然不能修改單個的。下面的初始化也只是例子而已。

                List<RoleItem> roles = new List<RoleItem>();// 假設已經擷取到了資料                                foreach(var m in roles)                {                    listBox1.Items.Add(m);                }

  在點擊介面的編輯按鈕時,擷取listBox1的選中項。然後修改類中的RoleName 屬性,所以正常的代碼如下編寫:

 private void userButton5_Click(object sender, EventArgs e)        {            if (listBox1.SelectedItem is RoleItem role)            {                // 點擊了編輯按鈕並擷取了需要修改的資訊                role.RoleName = role.RoleName + "1";            }        }

  上述代碼為樣本,重要的是結果發現listBox1中的顯示沒有更新,然是資料確實更新了,但是即使調用了下面的三個重新整理方法也是無濟於事。

                        listBox1.Refresh();                        listBox1.Invalidate();                        listBox1.Update();

  但是我在調用listBox1.Items.Add(object obj)時卻會自動更新,即使翻看微軟的原始碼,也沒有找到哪裡可以重新整理顯示的方法,原始碼地址:

http://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/ListBox.cs,81e38e1c58b6d37c

       所以參數上述的Add方法可以重新整理,也就是整個變數重新賦值也可以重新整理,所以上述的編輯代碼變更成了

 private void userButton5_Click(object sender, EventArgs e)        {            if (listBox1.SelectedItem is RoleItem role)            {                // 點擊了編輯按鈕並擷取了需要修改的資訊                role.RoleName = role.RoleName + "1";                listBox1.Items[listBox1.SelectedIndex] = listBox1.Items[listBox1.SelectedIndex];            }        }

  暫時解決了方法,當點擊按鈕時,控制項的介面也會跟著重新整理,如果需要全部重新整理,就要迴圈操作。

 

 

 

這個問題來自開發一個簡單通用的C-S架構的架構模版,地址為https://github.com/dathlin/ClientServerProject

C# ListBox 子項資料更新

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.