views:

65

answers:

1

Is it possible to bind to a property of a property? Here is what I have:

    [Bindable(true)]
    public class DataClass
    {
        private string DescriptionValue = null;
        private Content DataContent Value = new Content();
        ....

        [Bindable(true)]
        public Content DataContent
        {
            get { return DataContent; }
            set { DataContent = value; }
        }

        [Bindable(true)]
        public string Description
        {
            get { return DescriptionValue; }
            set { DescriptionValue = value; }
        }
        ...
    }


    [Bindable(true)]
    public class Content
    {
        private object ContentValue = null;
        private Color StateBackColorValue;
        ...

        [Bindable(true)]
        public object Content
        {
            get { return ContentValue; }
            set { ContentValue = value; }
        }

        [Bindable(true)]
        public Color StateBackColor
        {
            get { return StateBackColorValue; }
            set { StateBackColorValue = value; }
        }
        ...
    }

Is it somehow possible to bind a control to DataContent.Content or any other property of the Content class? I know that I could introduce properties in DataContent class that map the Content class properties. I just wanted to know if hierarchical databinding with properties is possible.

A: 

What type of data-binding are you doing?

With simple binding (TextBox.Text to a single object, for example), yes, you can use "Foo.Bar.SomeProp" as the member. For PropertyGrid, you can mark the objects with [TypeConverter(typeof(ExpandableObjectConverter))] and it will work.

The tricky one is list binding (DataGridView etc); here, no: it doesn't flatten easily. You can do it if you go to great lengths (ITypedList etc), but it really isn't worth it - just add shim properties to the parent:

public string ChildName {
   get {return child == null ? "" : child.Name;} // and setter if you want
}
Marc Gravell