0

私が抱えている問題は、オブジェクトを更新すると、ListBox が自動的にオブジェクトを削除してからリストに再度追加し、インデックスと値の変更イベントを呼び出すことです。カスタム ListBox コントロールを作成することでこれを防ぐことができ、PropertyChangedEvent が呼び出されたときに、基本クラスのイベントが呼び出されないようにするフラグを立てました。現在起こっていることは、参照全体が新しい参照に置き換えられており、ListBox でアイテムを再選択しない限り、参照が間違っているということです。

私が基本的にやりたいことは、オブジェクトの表示値を変更してから、リスト ボックス内のテキストのみを更新することです。オブジェクト/参照/それが行うことは何でも、削除して再追加したくありません。それはかなり迷惑です。

ここに私が取り組んでいるサンプルコードがあります...

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

1 に答える 1

0

これがINotifyPropertyChangedインターフェースの目的です。

カスタムイベントを発生させる代わりに、変更したプロパティの名前をイベント引数に設定してPropertyChangedイベントを発生させると、リストボックスが更新されます。

MSDNを参照してください。

于 2013-03-19T18:54:53.983 に答える