ControlsBinderDesignSupport.cs :  » Persistence-Frameworks » Data-Holder » DBWebControls » Controls » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Persistence Frameworks » Data Holder 
Data Holder » DBWebControls » Controls » ControlsBinderDesignSupport.cs
using System;
using System.Reflection;
using System.Globalization;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Web.UI.WebControls;
using System.ComponentModel.Design;
using System.Drawing.Design;
using System.ComponentModel.Design.Serialization;

namespace DBWebControls.Controls{
  /// <summary>
  /// Summary description for ControlsBinderDesignSupport.
  /// </summary>
  [Serializable]
  [TypeConverter(typeof(BindingTemplateTypeConverter))]
  public class BindingTemplate
  {
    private static bool DesignMode = true;
    private System.Web.UI.Control _BindedControl;
    private string _ControlProperty;
    private string _DataProperty;

    public BindingTemplate()
    {

    }

    [TypeConverter(typeof(ComponentConverter))]
    [Category("Binding")]
    [Description("The control to which the binding is applied")]
    [Bindable(false)]
    [Localizable(false)]
    public System.Web.UI.Control BindedControl
    {
      get{return _BindedControl;}
      set
      {
        _BindedControl = value;
        if(DesignMode && _BindedControl != null)
        {
          if( _ControlProperty == null || _ControlProperty.Length == 0)
            SetDefaultControlPropertyForControl();
          else
          {
            Type ctrlType = _BindedControl.GetType();
            if(ctrlType.GetProperty(_ControlProperty) == null)
              SetDefaultControlPropertyForControl();
          }
        }
      }
    }

    private void SetDefaultControlPropertyForControl()
    {
      if(_BindedControl is TextBox)
        _ControlProperty = "Text";
      else if(_BindedControl is DropDownList)
        _ControlProperty = "SelectedValue";
      else if(_BindedControl is CheckBox)
        _ControlProperty = "Checked";
    }

    public object GetControlValue()
    {
      if(BindedControl == null)
      {
        if(DataProperty != null)
          throw new Exception("Control binder Exception BindedControl is not set for " + DataProperty  +" !");
        else
          throw new Exception("Control binder Exception BindedControl is not set!");
      }
      return BindedControl.GetType().GetProperty(ControlProperty).GetValue(BindedControl, null);
    }

    public void SetControlValue(object val)
    {
      if(BindedControl == null)
      {
        if(DataProperty != null)
          throw new Exception("Control binder Exception BindedControl is not set for " + DataProperty  +" !");
        else
          throw new Exception("Control binder Exception BindedControl is not set!");
      }
      PropertyInfo pinfo = BindedControl.GetType().GetProperty(ControlProperty);
      pinfo.SetValue(BindedControl, Convert.ChangeType(val, pinfo.PropertyType), null);
    }

    [Category("Binding")]
    [Description("The Property of the control binded to the datasource")]
    [Bindable(false)]
    [Localizable(false)]
    public string ControlProperty
    {
      get{return _ControlProperty;}
      set{_ControlProperty = value;}
    }

    [Category("Binding")]
    [Description("The datasource column..")]
    [Bindable(false)]
    [Localizable(false)]
    public string DataProperty
    {
      get{return _DataProperty;}
      set{_DataProperty = value;}
    }

    public override string ToString()
    {
      string ret = "Bind ";
      if(DataProperty != null)
        ret += DataProperty;
      if(ControlProperty != null)
        ret += ControlProperty;
      return base.ToString ();
    }

    public static void Init()
    {
      DesignMode = false;
    }

  }
  
  public class BindingTemplateTypeConverter : TypeConverter
  {
    public override bool CanConvertTo(ITypeDescriptorContext context, Type destType)
    {
      if (destType == typeof(InstanceDescriptor))
      {
        return true;
      }
      return base.CanConvertTo(context, destType);
    }

    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destType)
    {
      if (destType == typeof(InstanceDescriptor))
      {
        ConstructorInfo info1 = typeof(BindingTemplate).GetConstructor(new Type[0]);
        return new InstanceDescriptor(info1, null, false);
      }
      return base.ConvertTo(context, culture, value, destType);
    }
  }

  public class BindingTemplateUIEditor:UITypeEditor
  {
    public override object EditValue(ITypeDescriptorContext ctx, IServiceProvider provider, object value)
    {
      if (((ctx == null) || (ctx.Instance == null)) || (provider == null))
      {
        return value;
      }
      UITypeEditor editor1 = new UITypeEditor();
//      BindingTemplateCollection collection1 = (BindingTemplateCollection) value;
//      IWindowsFormsEditorService service1 = (IWindowsFormsEditorService) provider.GetService(typeof(IWindowsFormsEditorService));
      
      return editor1.EditValue( provider, value);
      
//      DialogResult result1 = service1.ShowDialog(editor1);
//      return collection1;
    }
 

  }

  public class BindingTemplateCollection:CollectionBase
  {
    public int Add(BindingTemplate value)
    {
      return base.List.Add(value);
    }
    public void AddRange(BindingTemplateCollection value)
    {
      foreach (BindingTemplate item1 in value)
        this.Add(item1);
    }
    public bool Contains(BindingTemplate value)
    {
      return base.List.Contains(value);
    }
 
    public void CopyTo(BindingTemplate[] array, int index)
    {
      base.List.CopyTo(array, index);
    }
    public int IndexOf(BindingTemplate value)
    {
      return base.List.IndexOf(value);
    }
    public void Insert(int index, BindingTemplate value)
    {
      base.List.Insert(index, value);
    }
    public void Remove(BindingTemplate value)
    {
      base.List.Remove(value);
    }

    public BindingTemplate this[int index]
    {
      get
      {
        return (BindingTemplate) base.List[index];
      }
      set
      {
        base.List[index] = value;
      }
    }
  }

  public class ControlsBinderDesignSupport : System.ComponentModel.Design.CollectionEditor 
  {
    private Type[] types; 
    public ControlsBinderDesignSupport(Type type) : base(type)
    {
      types = new Type[] {typeof(BindingTemplate)};
    }

    protected override System.Type[] CreateNewItemTypes() 
    {
      return types;
    }
  }
}

www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.