代码之家  ›  专栏  ›  技术社区  ›  uli78

具有属性的层次结构数据绑定是否可能?

  •  1
  • uli78  · 技术社区  · 17 年前

    是否可以绑定到属性的属性? 以下是我的资料:

        [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; }
            }
            ...
        }
    

    是否可以将控件绑定到内容类的DataContent.Content或任何其他属性?我知道我可以在DataContent类中引入映射内容类属性的属性。我只是想知道是否可以使用属性进行分层数据绑定。

    1 回复  |  直到 17 年前
        1
  •  1
  •   Marc Gravell    17 年前

    您正在进行什么类型的数据绑定?

    带简单绑定( TextBox.Text 例如,对于单个对象),可以使用“foo.bar.someprop”作为成员。为了 PropertyGrid ,可以用标记对象 [TypeConverter(typeof(ExpandableObjectConverter))] 它会起作用的。

    棘手的是列表绑定( DataGridView 等等);这里,不:它不容易压扁。如果你去 伟大的 长度(长度) ITypedList 等等),但它确实不值得-只需向父级添加垫片属性:

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