Exemple #1
0
        public void AddBinding(Control control, string propertyName, INotifyPropertyChanged dataSource, string dataMember, DataSourceUpdateMode dataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged)
        {
            Binding binding = new Binding(propertyName, dataSource, dataMember);

            binding.ControlUpdateMode    = ControlUpdateMode.OnPropertyChanged;
            binding.DataSourceUpdateMode = dataSourceUpdateMode;

            control.DataBindings.Add(binding);
            // 对同一个DataSource,仅需要注册一次事件Handler
            if (!m_RegisteredDataSource.Contains(dataSource))
            {
                dataSource.PropertyChanged += new PropertyChangedEventHandler(dataSource_PropertyChanged);
                m_RegisteredDataSource.Add(dataSource);
            }

            // build mapping from datasource + datamember -> control + propertyname
            string key = GetKey(dataSource, dataMember);

            List <DataBindingCtrlInfo> bindingCtrlInfos;
            DataBindingCtrlInfo        bindingCtrlInfo = new DataBindingCtrlInfo(control, propertyName, dataSource);

            if (m_DTDataSource2ControlProperty.ContainsKey(key))
            {
                bindingCtrlInfos = m_DTDataSource2ControlProperty[key];
                bindingCtrlInfos.Add(bindingCtrlInfo);
            }
            else
            {
                bindingCtrlInfos = new List <DataBindingCtrlInfo>();
                bindingCtrlInfos.Add(bindingCtrlInfo);
                m_DTDataSource2ControlProperty.Add(key, bindingCtrlInfos);
            }

            // MyLog4Net.Container.Instance.Log.DebugFormat("Add Binding key {0}, control: {1}, type {2}", key, control.GetHashCode(), control.Name);
        }
Exemple #2
0
 private void HandleDataSourceChange(object sender, PropertyChangedEventArgs e, DataBindingCtrlInfo bindingCtrlInfo)
 {
     if (bindingCtrlInfo != null)
     {
         object val = sender.GetType().GetProperty(e.PropertyName).GetValue(sender, null);
         if (string.Compare(bindingCtrlInfo.PropertyName, "Visible", true) == 0)
         {
             // 修改Visible 属性不生效, 这里改为调用 Show, Hide 方法
             Control ctrl = bindingCtrlInfo.Ctrl;
             bool    show;
             if (Boolean.TryParse(val.ToString(), out show))
             {
                 if (show)
                 {
                     ctrl.Show();
                 }
                 else
                 {
                     ctrl.Hide();
                 }
             }
         }
         else
         {
             if (string.Compare(bindingCtrlInfo.PropertyName, "Text", true) == 0 && val != null)
             {
                 bindingCtrlInfo.Ctrl.GetType().GetProperty(bindingCtrlInfo.PropertyName).SetValue(
                     bindingCtrlInfo.Ctrl, val.ToString(), null);
             }
             else
             {
                 bindingCtrlInfo.Ctrl.GetType().GetProperty(bindingCtrlInfo.PropertyName).SetValue(
                     bindingCtrlInfo.Ctrl, val, null);
             }
         }
     }
 }