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

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

问题描述

我有一个列表框,它是数据绑定到一组对象。
我想修改项目显示的方式,以显示用户在我的程序中这些对象之一是START对象。



我试图做这是以下方式,但是列表框不会自动更新。
无效的控件也无效。



我可以找到的唯一方法是完全删除数据绑定并重新添加。但是在我的情况下,这是不可取的。



有另一种方式吗?

  class Person:INotifyPropertyChanged 
{
public event PropertyChangedEventHandler PropertyChanged;

私有字符串_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;
}
}

这是管理列表和项目的类那就是开始

  class PersonManager 
{
public BindingList< Person>人{get;组; }
public Person StartPerson {get;组; }

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

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

在表单中,我使用以下代码

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


解决方案

我很确定问题是,当您这样做时,您有效地使Person.Name属性get访问器更改值(并且就UI的角度而言,就像一个设置访问器)。



但是,没有什么是更新绑定来说这是发生。如果PropertyChanged在设置开始时被调用,我相信这会更新。



它很笨重,但是你写的方式,我相信你可以添加它,使它工作(注意:我没有测试这个,所以可能〜有问题):

  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,但强制一个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天全站免登陆