-3

Ok.

I have a 'Person' class with such properties: PersonId, Name and Age. So far I've added 3 different people and set the DataContext from a List. My validation rules are working ok.

What I want to know is, when I enter the second person's name in the first TextBox I want the second TextBox to show this person Age.

For example, let's say I have this DataContext:

  • John, 25 years old
  • Paul, 30 years old
  • George, 28 years old

As soon as I type 'Paul', the second TextBox should change it's value to '30'; If I type 'John', the second TextBox should change to '25' and so on.

Thanks in advance, Tiago

trinaldi
  • 2,872
  • 2
  • 32
  • 37

1 Answers1

1

The basic idea is to connect Name and Age through a Person Property. In Name's setter, you search the person based on the name, if the result is not null, then assign it to Person. In Person's setter, set Age and notify UI. Code is like:

    private Person _selectedPerson;

    public Person SelectedPerson
    {
        get { return _selectedPerson; }
        set
        {
            if (value == null)
                return;
            _selectedPerson = value;
            PersonAge = _selectedPerson.Age;
            OnPropertyChanged("Person");
        }
    }

    private string _personName;

    public string PersonName
    {
        get { return _personName; }
        set
        {
            _personName = value;
            var person = Persons.FirstOrDefault(item => item.Name == _personName);
            if (person != null)
                SelectedPerson = person;
            OnPropertyChanged("PersonName");
        }
    }


    private int _personAge;

    public int PersonAge
    {
        get { return _personAge; }
        set
        {
            _personAge = value;
            OnPropertyChanged("PersonAge");
        }
    }

In above code, Persons is the list you bind to ListBox, PersonName is bound to the textbox of name, PersonAge is bound to the textbox of age. I keep PersonAge writable here since you are using a textbox instead of a textblock.

Hope it can help.

Bill Zhang
  • 1,909
  • 13
  • 10
  • The thing is, I'm using a 3 layer architecture for this little example like so: Classes Layer, Business Logic Layer and Data Access Layer. Whenever I try to use the 'var person = Persons.FirstOrDefault(item => item.Name == _personName);' I'm getting an error about not implicit convert one layer to another. – trinaldi Jul 24 '13 at 14:19
  • Since no code, I am not sure what is your Persons type and which type you choose to bind to UI. Probably you have Person objects in both DataModel and Dto, and you mix them in one tier. – Bill Zhang Jul 24 '13 at 14:30
  • Here's my Person.cs: [Pastebin - Person.cs](http://pastebin.com/xAQKKbvj) – trinaldi Jul 24 '13 at 14:56
  • You should not put SelectedPerson inside Person class. You might have a class containing a list of person which is data bound to UI. You'd better put SelectedPerson property inside that class. – Bill Zhang Jul 24 '13 at 15:22
  • Thanks, I'll try and do that. I'm not using MVVM for this project, maybe I should consider this for now. – trinaldi Jul 24 '13 at 16:57
  • Bill, after all this time i get what you said! Thanks A LOT! – trinaldi Oct 06 '13 at 19:55