Search code examples
c#wpfdatagrid

Bind object properties to a datagrid in WPF


I have the following class:

public class Sp3dItem
{
    public Sp3dItem()
    {
        Items= new ObservableCollection<Sp3dItem>();
    }

    public string OID
    {
        get;
        set;
    }

    public string Name
    {
        get;
        set;
    }

    public string Type
    {
        get;
        set;
    }

    public ObservableCollection<Sp3dItem> Items
    {
        get;
        set;
    }
}

I need to show the properties of an instance of this object to a Datagrid (or any other type of grid). Like the Properties Window in Visual Studio. But there are certain properties that I don't care, like 'Items', I only need to show properties of string Type, and only the ones with non empty values (this last one would be a plus, not a real need).

The question is, can I do something like this with binding or do I have to assembly the data on the grid manually?


Solution

  • Sounds like you want a property grid to view the properties of a single object instance, where each property/value pair is a 'row', yes? If that's the case, look into some of the third-party Property Grid controls. The WPF Extended Toolkit has a free one.

    Typically, these grids can automatically discover the properties of the target object, and you can choose to hide certain properties by adorning them with [Browsable(false)].