开发者

WPF DataGrid Entity Framework: Is it possible to bind a datagrid column to a method/function?

开发者 https://www.devze.com 2022-12-30 10:35 出处:网络
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 bot开发者_开发技巧h inherit the abstract e

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 bot开发者_开发技巧h 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? :-)


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
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消