PropertyGrid中不确定下拉框数据绑定

liyoung1991 2012-09-24 10:21:10
private string theName = string.Empty;
private object theValue = null;
当theValue为数组时 这一栏就设置为下拉框 并把数组的值绑定到下拉框中
propertyg中每行前面一栏为theName的值 后面一栏为theValue的值
这个怎么实现 求大神指教
...全文
501 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
hemozi 2013-03-05
  • 打赏
  • 举报
回复
对于自定义属性编辑器,想请教各位一个问题,想实现用PropertyGrid实现下拉列表选项为图片的形式,自定义属性编辑器,应该继承自哪个类?override哪个方法呢?需要用到枚举吗?请各位大神给个思路,越详细越好!
机器人 2012-09-25
  • 打赏
  • 举报
回复
呵呵,到这个节骨眼了没想明白么?

ListAttribute(strArray)
这里可以大作文章啊。比如传入 xpath,到指定的xml读取。
或者获得某个静态类的数组返回。

public class ListAttribute : Attribute
{
public string[] _lst;

public ListAttribute(string[] lst)
{
//初始化列表值
_lst = new[] {"1", "3", "4"};
}
}

qldsrx 2012-09-25
  • 打赏
  • 举报
回复
调用代码不是写了吗?在属性上面添加[Editor(typeof(SysMenuSelectorEditor), typeof(UITypeEditor))]

至于下拉框的内容,我示例里面是来自那个CommonData.sysmenus.Keys,而你的情况就是访问外部的那个theValue 变量,遍历添加,如果想做得更加灵活点,可以模仿前面的代码,使用特性访问来选择外部变量。
liyoung1991 2012-09-25
  • 打赏
  • 举报
回复
我再问个问题。。。这里面的分 是怎么得的 我没有分了。。。我想办法挣点分去 这还是我第一次发帖
liyoung1991 2012-09-24
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]

看来你表达问题的能力不行,如果只是要自定义属性编辑器,那个很好解决,而你一开始的描述似乎就是要让PropertyGrid能够随便添加行,“propertyg中每行前面一栏为theName的值 后面一栏为theValue的值
”不就是说要给一个theName就添加一行栏目吗?那就完全和类的绑定无关了。

对于自定义属性编辑器,继承UITypeEditor即可,而你这里是对数组的下拉框,则是继……
[/Quote]请问一下这一块代码怎么调用。。非常感谢
qldsrx 2012-09-24
  • 打赏
  • 举报
回复
看来你表达问题的能力不行,如果只是要自定义属性编辑器,那个很好解决,而你一开始的描述似乎就是要让PropertyGrid能够随便添加行,“propertyg中每行前面一栏为theName的值 后面一栏为theValue的值
”不就是说要给一个theName就添加一行栏目吗?那就完全和类的绑定无关了。

对于自定义属性编辑器,继承UITypeEditor即可,而你这里是对数组的下拉框,则是继承ObjectSelectorEditor 并重写FillTreeWithData方法,对于上面给出的继承TypeConverter虽然看似达到了目的,其实并非理想,理想的应该使用Edit特性,如:
[Editor(typeof(SysMenuSelectorEditor), typeof(UITypeEditor))]

internal class SysMenuSelectorEditor : ObjectSelectorEditor
{
protected override void FillTreeWithData(Selector theSel,
ITypeDescriptorContext theCtx, IServiceProvider theProvider)
{
base.FillTreeWithData(theSel, theCtx, theProvider); //clear the selection

theSel.Nodes.Add(new SelectorNode("(无)", ""));
if (CommonData.sysmenus != null)
foreach (String aIt in CommonData.sysmenus.Keys)
{
SelectorNode aNd = new SelectorNode(aIt, aIt);

theSel.Nodes.Add(aNd);

if (aIt == (String)theCtx.PropertyDescriptor.GetValue(theCtx.Instance))
theSel.SelectedNode = aNd;
}
}
}
liyoung1991 2012-09-24
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

http://www.cnblogs.com/2008freestyle/archive/2010/04/30/1724910.html
[/Quote]
private string _name;
private static string[] strArray = new string[] { "张三", "李四", "王五", "赵六", "马七" };

[CategoryAttribute("信息"), DescriptionAttribute("姓名"),
TypeConverter(typeof(MyConverter)), ListAttribute(strArray)]
public string Name
{
get { return _name; }
set { _name = value; }
}
}
不能传数组进去。。。
错误 1 特性实参必须是特性形参类型的常量表达式、typeof 表达式或数组创建表达式
liyoung1991 2012-09-24
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

http://www.cnblogs.com/2008freestyle/archive/2010/04/30/1724910.html
[/Quote][CategoryAttribute("信息"), DescriptionAttribute("姓名"),
52 TypeConverter(typeof(MyConverter)), ListAttribute( new string[] { "张三", "李四", "王五", "赵六", "马七" })] 这里面的这个数组可以写活吗? 可以传一个数组进去不 这种东西我不是很懂 谢谢你
liyoung1991 2012-09-24
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

没法实现,除非自定义控件,那个PropertyGrid只能识别类的属性,无法自己添加栏目。
[/Quote]嗯 所以我在网上扒了一段代码 但是还有些问题没有解决了。。。。
liyoung1991 2012-09-24
  • 打赏
  • 举报
回复
<?xml version="1.0" encoding="utf-8" ?>
<PropertyInfo>
<Propertys GroupID="1">
<Property PropertyName="属性1" PropertyValue="背影色"/>
<Property PropertyName="属性2" PropertyValue="太坑爹了"/>
<Property PropertyName="属性3" PropertyValue="字体"/>
<Property PropertyName="属性4" ListId="2" />
<Property PropertyName="属性5" PropertyValue="内容" />
<Property PropertyName="ScrollBars" PropertyValue="滚动条" />
<Property PropertyName="TextAlign" PropertyValue="文本对齐" />
<Property PropertyName="Multline" PropertyValue="多行" />
<Property PropertyName="PasswordChar" PropertyValue="密码文本" />
<Property PropertyName="Size" PropertyValue="大小" />
<Property PropertyName="Location" PropertyValue="位置" />
</Propertys>

<ValueList ListId="1">
<List PropertyValue="可选值1"/>
<List PropertyValue="可选值2"/>
<List PropertyValue="可选值3"/>
<List PropertyValue="可选值4"/>
</ValueList>
<ValueList ListId="2">
<List PropertyValue="可选值5"/>
<List PropertyValue="可选值6"/>
<List PropertyValue="可选值7"/>
<List PropertyValue="可选值8"/>
</ValueList>
</PropertyInfo>
liyoung1991 2012-09-24
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace EX1
{
public class Demo
{
private object[] obj;
public static string prophead = string.Empty;
public object propvaule;
//Demo de = new Demo();

public Demo() { }
public Demo(object[] obj)
{
prophead = obj[0].ToString();
propvaule = obj[1];
}

private int theCountry;
private bool theLanguage;

[CategoryAttribute("基本信息"), DescriptionAttribute("如果你要将用户设置为删除状态,请选择“是”"), DisplayName("删除状态")]
[TypeConverter(typeof(Class2))]
public int Country
{
get
{
return theCountry;
}
set
{
theCountry = value;
}
}

[CategoryAttribute("基本信息"), DescriptionAttribute("如果你要将用户设置为锁定状态,请选择“是”"), DisplayName("锁定状态")]
public bool Language
{
get
{
return theLanguage;
}
set
{
theLanguage = value;
}
}

[CategoryAttribute("基本信息"), DescriptionAttribute("如果你要将用户设置为锁定状态,请选择“是”"), DisplayName("Location")]
public object Obj
{
get
{
return propvaule;
}
set
{
propvaule = value;
}

}
}
}







using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace EX1
{
public class Class2 : ComboBoxItemTypeConvert
{
Hashtable hash = new Hashtable();
public override void GetConvertHash()
{

_hash = this.hash;
//_hash.Add("零", 0);
//_hash.Add("一", 1);
//_hash.Add("二", 2);
//_hash.Add("三", 3);
//_hash.Add("四", 4);
}
public void ListToHash(string[] list)
{
for (int i = 0; i < list.Length; i++)
{
hash.Add(list[i].ToString(), i);
}
}
}
}







using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections;

namespace EX1
{
public abstract class ComboBoxItemTypeConvert : TypeConverter
{
public Hashtable _hash;

public abstract void GetConvertHash();

public ComboBoxItemTypeConvert()
{
_hash = new Hashtable();
GetConvertHash();
}

public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}

public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
string[] ids = new string[_hash.Values.Count];

int i = 0;
foreach (DictionaryEntry myDE in _hash)
{
ids[i++] = (string)(myDE.Key);
}

return new StandardValuesCollection(ids);
}

public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
return true;

return base.CanConvertFrom(context, sourceType);
}

public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if (value is string)
{
foreach (DictionaryEntry myDe in _hash)
{
if (myDe.Key.Equals(value))
return myDe.Value;
}
}
return base.ConvertFrom(context, culture, value);
}

public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object v, Type destinationType)
{
if (destinationType == typeof(string))
{
foreach (DictionaryEntry myDE in _hash)
{
if (myDE.Value.Equals(v))
return myDE.Key.ToString();
}
}
return base.ConvertTo(context, culture, v, destinationType);

}

public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
{
return true;
}
}
}






using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using DevComponents.DotNetBar;

namespace EX1
{
public partial class Form1 : Form
{
XmlDocument mXDoc = new XmlDocument();
public Form1()
{
InitializeComponent();
Load();
//Demo o = new Demo();
//this.advPropertyGrid1.SelectedObject= o; //”propertyGrid1“为PropertyGrid控件
}
public void Load()
{

mXDoc.Load(Application.StartupPath + "/xmlPropertyInfo.xml");
GetProperty();
}

public void GetProperty()
{
XmlNode tmpXNode = mXDoc.SelectSingleNode("PropertyInfo/Propertys[@GroupID=\"1\"]");
XmlNodeList tmpXPropLst = tmpXNode.SelectNodes("Property");

AdvPropertyGrid progrid = new AdvPropertyGrid();
progrid = this.advPropertyGrid1;

foreach (XmlNode node in tmpXPropLst)
{
Class2 ex = new Class2();
object[] list = new object[2];
Demo de = new Demo();

list[0] = node.Attributes["PropertyName"].Value;

if (node.Attributes["PropertyValue"] != null)
{
list[1] = node.Attributes["PropertyValue"].Value;
de.Obj = node.Attributes["PropertyValue"].Value;
progrid.SelectedObject = de;
}
else
{
string[] str= GetValueList(node.Attributes["ListId"].Value);
list[1]=str;
ex.ListToHash((string[])list[1]);
progrid.SelectedObject = new Demo((string[])list[1]);
}


}
}

public string[] GetValueList(string ListId)
{
XmlNode tmpXNode = mXDoc.SelectSingleNode("PropertyInfo/ValueList[@ListId=\"" + ListId + "\"]");
XmlNodeList tmpXPropLst = tmpXNode.SelectNodes("List");
string[] valueList = new string[tmpXPropLst.Count];
int i = 0;
foreach (XmlNode node in tmpXPropLst)
{
string strValue = node.Attributes["PropertyValue"].Value;
valueList[i] = strValue;
i++;
}
return valueList;
}
}
}
liyoung1991 2012-09-24
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

PropertyGrid 可以直接Edit类型。

但我没看明白:“propertyg中每行前面一栏为theName的值 后面一栏为theValue的值” 这是什么意思。
[/Quote]
属性1 | 值1
属性2 | 数组1(下拉框显示)
属性3 | 数组3(下拉框显示)
我的意思就是属性1,属性2等也是数据绑定的 后边的也是绑定的 属性1这些是theName的值 数组这些都是 theValue的值 我把代码贴出来。。。谢谢帮忙 谢谢
机器人 2012-09-24
  • 打赏
  • 举报
回复
PropertyGrid 可以直接Edit类型。

但我没看明白:“propertyg中每行前面一栏为theName的值 后面一栏为theValue的值” 这是什么意思。
qldsrx 2012-09-24
  • 打赏
  • 举报
回复
没法实现,除非自定义控件,那个PropertyGrid只能识别类的属性,无法自己添加栏目。

110,500

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧