如何更新与INotifyPropertyChanged的列表框项目 [英] how to update listbox items with INotifyPropertyChanged

查看:169
本文介绍了如何更新与INotifyPropertyChanged的列表框项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据绑定对象的集合列表框。
我想修改的项目显示,以显示这些对象之一是我在程序的起始对象用户的方式。



我试着做这通过以下方式,但在列表框中不自动更新。
作废的控制也没有工作。



我能找到的唯一方法是完全删除数据绑定,并重新添加。但对我来说这是不可取的。



有另一种方式?



 类人:INotifyPropertyChanged的
{
公共事件PropertyChangedEventHandler的PropertyChanged;

私人字符串_name;
公共字符串名称
{
得到
{
如果(PersonManager.Instance.StartPerson ==本)
返回_name +(开始);
返回_name;
}

{
_name =价值;
如果(的PropertyChanged!= NULL)
的PropertyChanged(这一点,新PropertyChangedEventArgs(姓名));
}
}

公众人物(字符串名称)
{
名称=名称;
}
}

这是类至极管理列表和项目这是启动

 类的PersonManager 
{
公众的BindingList<&人GT;人{搞定;组; }
公众人物StartPerson {搞定;组; }

私有静态的PersonManager _instance;
公共静态的PersonManager实例
{
得到
{
如果(_instance == NULL)
{
_instance =新的PersonManager();
}
返回_instance;
}
}

私人的PersonManager()
{
=人新的BindingList<&人GT;();
}
}

在我使用下面的代码的形式

 私人无效的button1_Click(对象发件人,EventArgs五)
{
PersonManager.Instance.StartPerson =(人)listBox1中.SelectedItem;
}


解决方案

我敢肯定,问题是,当你这样做,你就有效地使Person.Name特性得到访问更改值(和尽可能的UI关注像set访问)。



然而,没有什么是更新绑定地说,这种情况正在发生。如果的PropertyChanged接到电话,当您设置开始,我相信这将更新。



这是笨重的,但你拥有了它编写的方式,我相信你可以添加此并使其工作(注:我没有测试这一点,所以它可能〜〜有问题):

 私人无效的button1_Click(对象发件人,EventArgs五)
{
的人NEWSTART =(人)listBox1.SelectedItem;
如果(新起点!= NULL)
{
PersonManager.Instance.StartPerson =新起点;
newStart.Name = newStart.Name; //哑巴,但强制PropertyChanged事件使绑定更新
}
}


I have a listbox which is databound to a collection of objects. I want to modify the way the items are displayed to show the user which one of these objects is the START object in my program.

I tried to do this the following way, but the listbox does not automatically update. Invalidating the control also didn't work.

The only way I can find is to completely remove the databindings and add it back again. but in my case that is not desirable.

Is there another way?

class Person : INotifyPropertyChanged
{
	public event PropertyChangedEventHandler PropertyChanged;

	private string _name;
	public string Name
	{
		get
		{
			if (PersonManager.Instance.StartPerson == this)
				return _name + " (Start)";						
			return _name;
		}
		set
		{
			_name = value;
			if (PropertyChanged != null)
				PropertyChanged(this, new PropertyChangedEventArgs("Name"));
		}
	}

	public Person(string name)
	{
		Name = name;
	}
}

This is the class wich manages the list and the item that is the start

class PersonManager
{
	public BindingList<Person> persons { get; set; }
	public Person StartPerson { get; set; }

	private static PersonManager _instance;
	public static PersonManager Instance
	{
		get
		{
			if (_instance == null)
			{
				_instance = new PersonManager();
			}
			return _instance;
		}
	}

	private PersonManager()
	{
		persons = new BindingList<Person>();
	}
}

In the form I use the following code

	private void button1_Click(object sender, EventArgs e)
	{
		PersonManager.Instance.StartPerson = (Person)listBox1.SelectedItem;
	}

解决方案

I'm pretty sure that the problem is that, when you do this, you're effectively making the Person.Name properties "get" accessor change the value (and act like a set accessor as far as the UI is concerned).

However, there is nothing that's updating the bindings to say that this is happening. If PropertyChanged got called when you set start, I believe this would update.

It's clunky, but the way you have it written, I believe you could add this and make it work (NOTE: I didn't test this, so it ~may~ have issues):

private void button1_Click(object sender, EventArgs e)
{
    Person newStart = (Person)listBox1.SelectedItem;
    if (newStart != null)
    {
        PersonManager.Instance.StartPerson = newStart;
        newStart.Name = newStart.Name; // Dumb, but forces a PropertyChanged event so the binding updates
    }
}

这篇关于如何更新与INotifyPropertyChanged的列表框项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆