日期:2014-05-19  浏览次数:20849 次

自定义控件添加属性
又得请教各位高手了,
      自定义控件添加属性,我现在想添加两个属性,达到一种连动,
当我在第一个属性选择时,第二个属性的选择项是跟着变化的,
例如:当我在第一个属性的下拉列表框选择A时,第二个属性的下拉框里面变成K,L,D这三项供选择
            当我在第一个属性的下拉列表框选择B时,第二个属性的下拉框里面变成R,Y,P这三项供选择

先谢谢了,哪位帮忙啊,
不胜感激!

------解决方案--------------------
http://www.codeproject.com/csharp/dropdownproperties.asp
------解决方案--------------------
// 比较常规的代码模型

public SomeType PropertyA
{
get;
set { OnPropertyAChanged(null); // ... }
}

private void OnPropertyAChanged(EventArgs e)
{
// here changes property B
}
------解决方案--------------------
以TextBox为例
using System;
using System.Windows.Forms;
using System.Drawing.Design;
using System.ComponentModel;
using System.Globalization;
using System.Windows.Forms.Design;
public class TextBoxEx:TextBox
{
private All a= All.A;
private object b;
public All A
{
get
{
return a;
}
set
{
a=value;
switch(value)
{
case All.A:
b=Aenum.D;
break;
case All.B:
b=Benum.P;
break;
}
}
}

[System.ComponentModel.Editor(typeof(EnumEditer),typeof(UITypeEditor))]
public object B
{
get
{
return b;
}
set
{
b=value;
}
}
}

public class EnumEditer:UITypeEditor
{
ListBox dropdown ;
IWindowsFormsEditorService edSvc;
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object Value)
{
if (provider != null && Value !=null)
{
edSvc = (IWindowsFormsEditorService) provider.GetService(typeof(IWindowsFormsEditorService));
if ((edSvc ==null) || (context.Instance == null))
{
return Value;
}
if( dropdown ==null)
{
dropdown = new ListBox();
dropdown.BorderStyle = BorderStyle.None;
dropdown.MouseUp+= new System.Windows.Forms.MouseEventHandler(dropdown_MouseUp);
}
dropdown.Items.Clear();
Array values= System.Enum.GetValues(Value.GetType());
for(int i=0;i <values.Length;i++)
{
dropdown.Items.Add(values.GetValue(i));
}
edSvc.DropDownControl(this.dropdown);
Value = this.dropdown.SelectedItem;
return Value;
}
return Value;
}
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.DropDown;
}

private void dropdown_MouseUp(object sender , System.Windows.Forms.MouseEventArgs e)
{
if( edSvc != null && e.Button == MouseButtons.Left && this.dropdown.SelectedIndex > = 0)
{
edSvc.CloseDropDown();
}
}

}

public enum All
{
A,
B
}
public enum Aenum
{
K,
L,
D
}
public enum Benum
{
R,
Y,
P
}
------解决方案--------------------
没必要写那么多代码吧,要实现属性间的联动,只须要在属性的set中设置就行了
比如
public enum DragFlag
{
/// <summary>