views:

27

answers:

3

Please i need help:

i have a listbox binded to IEnumerable source

Person is a class that contains the following properties: Name (string), isChecked(bool)

i need to change the property "isChecked" for a specific person with the name "Bob"

i'm not able to change the value of the property!

please help

+1  A: 

If you are trying to have the displayed value updated when you update your model, then you probably need to implement INotifyPropertyChanged on your model class, e.g.

public class Person : INotifyPropertyChanged
{
    public string Name
    {
        get
        {
            return this.name;
        }
        set
        {
            this.name = value;
            this.FirePropertyChanged("Name");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void FirePropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
Simon Steele
A: 

I'm assuming you just need to change it once for the view?

Depending on how much data you have, the simplest way to implement would be to expand your data in the model into e.g. a List<>, then make the modification and bind your control to that list.

var people = myDataSource.ToList(); // a LINQ extension - you may need a 'using'
var bob = people.FirstOrDefault(p => p.Name == "Bob");
if (bob != null)
{
    bob.isChecked = true;
}

Alternatively, and probably better, would be to make the change on-the-fly using e.g. a Select()

var peopleWithBobTicked = myDataSource.Select(
    p => {
             if (p.Name == "BoB") p.isTicked = true;
             return p;
         });
Rup
A: 

Thaks Rup this is what i wanted!

maz313