using System;
using System.Reflection;
using System.Globalization;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Windows.Forms;
using System.ComponentModel.Design;
using System.Drawing.Design;
using System.ComponentModel.Design.Serialization;
namespace DBWinControls.Controls{
/// <summary>
/// Summary description for ControlsBinderDesignSupport.
/// </summary>
[Serializable]
[TypeConverter(typeof(BindingTemplateTypeConverter))]
public class BindingTemplate
{
private static bool DesignMode = true;
private 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 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 ComboBox)
_ControlProperty = "SelectedValue";
else if(_BindedControl is CheckBox)
_ControlProperty = "Checked";
}
[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;
}
}
}
|