【问题标题】:Databinding ApplicationSettings to Custom Components将 ApplicationSettings 数据绑定到自定义组件
【发布时间】:2010-09-09 16:30:51
【问题描述】:

我有一个已实现的自定义组件 INotifyPropertyChangedIBindableComponent

但是,当我尝试对属性进行数据绑定时,设计者会添加这个 行:

this.component11.TestString =
global::WindowsFormsApplication2.Properties.Settings.Default.Setting;

而不是像使用 TextBox 那样创建绑定:

this.textBox2.DataBindings.Add(new System.Windows.Forms.Binding(
   "Text",
   global::WindowsFormsApplication2.Properties.Settings.Default,
   "Setting2",
   true,
   System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));

我原以为设计师只是看看是否 IBindableComponent 已实现,如果是,则生成绑定 编码而不是分配代码。

任何想法为什么这适用于文本框而不是我的自定义组件?

这是我的自定义组件:

public partial class Component1 : Component, INotifyPropertyChanged, IBindableComponent
    {
        public Component1()
        {
            InitializeComponent();
        }

        public Component1(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
        }

        private string teststring;
        [Bindable(true)]
        public string TestString
        {
            get
            {
                return teststring;
            }
            set
            {
                if (teststring != value)
                {
                    teststring = value;
                    FirePropertyChanged("TestString");
                }
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        void FirePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region IBindableComponent Members

        private BindingContext bindingContext = null;

        public BindingContext BindingContext
        {
            get
            {
                if (null == bindingContext)
                {
                    bindingContext = new BindingContext();
                }

                return bindingContext;
            }
            set { bindingContext = value; }
        }

        private ControlBindingsCollection databindings;

        public ControlBindingsCollection DataBindings
        {
            get
            {
                if (null == databindings)
                {
                    databindings = new ControlBindingsCollection(this);
                }
                return databindings;
            }
            set { databindings = value; }
        }

        #endregion
    }

print("code sample");

【问题讨论】:

    标签: c# data-binding custom-component


    【解决方案1】:

    试试:

    [ DesignerSerializationVisibility( DesignerSerializationVisibility.Hidden ),
      EditorBrowsable( EditorBrowsableState.Advanced ),
      Browsable( false ) ]
    public BindingContext BindingContext {
        ...
    }
    
    [ ParenthesizePropertyName( true ),
      RefreshProperties( RefreshProperties.All ),
      DesignerSerializationVisibility( DesignerSerializationVisibility.Content ),
      Category( "Data" ) ]
    public ControlBindingsCollection DataBindings {
       ...
    }
    

    【讨论】:

    • 这似乎不起作用,设计器仍然生成代码:this.component11.TestString = global::TestApplicationSettings.Properties.Settings.Default.TestSetting;而不是正确的绑定代码。还有其他的想法吗?感谢您的回答。
    • 等等,确实有效。在进行建议的更改后,我只需要重新编译我的组件,删除并再次添加它。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-05
    • 2013-01-14
    相关资源
    最近更新 更多