2013-03-19 54 views
0

我遇到的問題是,當我更新我的對象時,ListBox會自動刪除,然後將該對象重新添加到列表中,從而調用索引和值更改的事件。我可以通過創建一個自定義的ListBox控件來防止這種情況,並且在調用PropertyChangedEvent時,我會引發一個標誌來阻止基類中的這些事件被調用。現在發生的事情是,我的整個引用正在被新的引用取代,除非我重新選擇列表框中的項目,否則我有錯誤的引用。我可以防止ListBox.RefreshItem(對象項)移除並重新添加對象嗎?

我基本上想要做的是更改我的對象中的顯示值,然後讓它只更新列表框中的文本。我不希望它刪除並重新添加對象/參考/不管它做什麼。這很煩人。

下面是示例代碼,我有工作...

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
     this.myListBox1.SelectedValueChanged += this.onchange; 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     this.myListBox1.Add(new strobj("z")); 
     this.myListBox1.Add(new strobj("a")); 
     this.myListBox1.Add(new strobj("b")); 
     this.myListBox1.Add(new strobj("f")); 
     this.myListBox1.Add(new strobj("n")); 
     this.myListBox1.Add(new strobj("h")); 
     this.myListBox1.Add(new strobj("p")); 
     this.myListBox1.Add(new strobj("t")); 
     this.myListBox1.Add(new strobj("c")); 
     this.myListBox1.Add(new strobj("q")); 
    } 

    private void onchange(object sender, EventArgs e) 
    { 
     MessageBox.Show("Hello World"); 
    } 

    int i = 0; 
    private void button1_Click(object sender, EventArgs e) 
    { 


     if (this.myListBox1.SelectedItem != null) 
     { 
      strobj item = (strobj)this.myListBox1.SelectedItem; 
      item.Name1 = i++.ToString(); 
     } 
    } 
} 

public partial class MyListBox 
{ 
    public MyListBox() 
    { 
     InitializeComponent(); 
    } 

    public void Add(strobj item) 
    { 
     item.OnNameChanged += this.MyDispalyMemberChanged; 
     this.Items.Add(item); 
    } 

    bool refreshing = false; 
    public void MyDispalyMemberChanged(strobj itemChanged) 
    { 
     this.refreshing = true; 
     this.RefreshItem(this.Items.IndexOf(itemChanged)); 
     this.refreshing = false; 
    } 

    protected override void OnSelectedValueChanged(EventArgs e) 
    { 
     if (!this.refreshing) 
     { 
      base.OnSelectedValueChanged(e); 
     } 
    } 
} 

class strobjCollection : List<strobj> 
{ 
    NameChangeEventHandler NameChangedEvent; 
} 

delegate void NameChangeEventHandler(strobj sender); 

public class strobj 
{ 
    internal NameChangeEventHandler OnNameChanged; 

    private string _Name1; 
    public string Name1 
    { 
     get { return this._Name1; } 
     set 
     { 
      this._Name1 = value; 
      if (this.OnNameChanged != null) 
      { 
       this.OnNameChanged(this); 
      } 
     } 
    } 

    public int i = 0; 
    public string str = "p"; 

    public strobj(string name) 
    { 
     this._Name1 = name; 
    } 

    public strobj() 
    { 
     this._Name1 = "You did not create this object"; 
    } 

    public override string ToString() 
    { 
     return this._Name1; 
    } 
} 

回答

0

這就是INotifyPropertyChanged接口被造的。

而不是提高你的自定義事件,你會引發PropertyChanged事件與您更改的屬性的名稱設置在事件參數和列表框將更新。

See MSDN.

+0

是的,但是這種方法仍然刪除並重新添加到列表的對象並調用SelectedValueChanged事件的兩倍。第一次將其更改爲空,第二次將其更改爲新值。現在我先前提取的引用不再引用當前選定的項目,很可能是因爲在我的實現版本中,我重寫了==運算符。我想要發生的唯一事情就是更新文本。我不希望該項目被刪除並重新添加。 – MJLaukala 2013-03-19 20:25:38

相關問題