views:

125

answers:

1

Hi.

I'm wondering if it's possible to bind a gridcolumn (field) to a method or function of an entity?

For example I have two entities Person and Company that both inherit the abstract entity Addressee. In my grid I'm listing all Addressees (both persons and companies). I have a column, Name, in the datagrid that I whish to bind to a function GetName(). This function is part of the entity Addressee and based on what type of addressee it is it returns CompanyName (if company) or FirstName+' '+LastName (if person).

I also have tried to add a partial class Addressee with a property Name that does the same thing as the function descried over, but this failes when I'm saving to database because the column Name does not exist in database.

Can anybody please help me? :-)

+1  A: 

That's how ViewModel was born. You should not alter your Model according to your View. Instead create a class that will

  • expose the Model's property that are required by the view
  • hold other derived properties.

public class AddresseeViewModel
{    
    private readonly Addressee addressee; //Your Model
    public string FirstName //Your FirstName Property
    {
        get
        {
            return addressee.FirstName;
        }
        set
        {
            addressee.FirstName = value;
            OnPropertyChanged("FirstName");
        }
    }
    public string Name
    {
        // Add your Name Property Logic
    }
}
Veer
Do you know how to use this in an n-tier application with poco objects?
seddler